Dynamic loading of python modules

后端 未结 6 1539
囚心锁ツ
囚心锁ツ 2020-11-27 13:01

In python how do you dynamically add modules to a package while your program is running.

I want to be able to add modules to the package directory from an outside pr

6条回答
  •  清酒与你
    2020-11-27 13:35

    Your code is almost correct.

    See __import__ function.

    def doSomething(name):
        name = "package." + name
        mod = __import__(name, fromlist=[''])
        mod.doSomething()
    

提交回复
热议问题