Mailing List Archive

[issue2090] __import__ with fromlist=[''] causes double initialization of modules
Mart Sõmermaa <mrts@mrts.pri.ee> added the comment:

A pointer for people who keep referring to this bug -- after
discussions, the following idiom was selected as the "official" way to
import modules by name in 2.x (as seen in latest 2.x docs
http://docs.python.org/dev/library/functions.html#__import__ ).

---

If you simply want to import a module (potentially within a package) by
name, you can get it from sys.modules:

>>> import sys
>>> name = 'foo.bar.baz'
>>> __import__(name)
<module 'foo' from ...>
>>> baz = sys.modules[name]
>>> baz
<module 'foo.bar.baz' from ...>

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue2090>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue2090] __import__ with fromlist=[''] causes double initialization of modules [ In reply to ]
Brett Cannon <brett@python.org> added the comment:

And just some more info, Python 2.7/3.1 have gained the importlib
module/package and its import_module function which gives a much saner
API than __import__.

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue2090>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com