开发者

How to import a class from a module from a package dynamically?

开发者 https://www.devze.com 2023-01-13 12:55 出处:网络
This is probably really simple, but I don\'t understand it. The following works: foo.py class Foo: pass bar.py

This is probably really simple, but I don't understand it. The following works:

foo.py

class Foo:
    pass

bar.py

module = __import__('foo')
foo = module.__dict__['Foo']

Afterwards, foo is the class foo.Foo as expected.


Yet, if I put a.py into a package, it stops working:

qux/__init__.py

(empty file)

qux/foo.py

class Foo:
    pass

bar.py

module = __import__('qux.foo')开发者_开发百科
foo = module.__dict__['Foo']

Running python bar.py gives me KeyError: 'Foo', but the module import is still successful.

What's going on, and how do I get it to work?


__import__ applied to nested module names returns the toplevel module/package - which is qux here. All modules are, however, inserted into sys.modules, so you can simply lookup qux.foo in there.

See the PyDoc on __import__() -it's all described there.


If you want to import a submodule, you can do something like this:

package = __import__('qux', fromlist=['foo'])
module = getattr(package, 'foo')

Note that with a fromlist the __import__ returns the most nested name in the first parameter, so you could substitute baz.bar.qux in the __import__ call to access the baz.bar.qux.foo module.


You need to use the fromlist parameter to reference a submodule:

temp = __import__('qux.foo', globals(), locals(), ['Foo'], -1)
foo = temp.Foo


You can directly access a module, nested or not, using importlib:

import importlib
module_name="qux.foo"
mod=importlib.import_module(module_name)

See http://docs.python.org/dev/library/importlib.html for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消