Importing a python module without actually executing it

后端 未结 6 2018
抹茶落季
抹茶落季 2020-12-10 01:48

In the context of a complex application, I need to import user-supplied \'scripts\'. Ideally, a script would have

def init():
    blah

def execute():
    mo         


        
6条回答
  •  一整个雨季
    2020-12-10 02:21

    I'd first of all not require some functions, but a class that conforms to a specified interface, using either the abc module, or zope.interface. This forces the maker of the module to supply the functions you want.

    Secondly, I would not bother looking for module-level code. It's the module-makers problem if he does this. It's too much work with no actual benefit.

    If you are worried about security issues, you need to sandbox the code somehow anyway.

提交回复
热议问题