classification
Title: __import__ with fromlist=[''] causes double initialization of modules
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.6, Python 2.5
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, hauser, mrts (3)
Priority: normal Keywords

Created on 2008-02-12 20:50 by hauser, last changed 2009-02-02 20:10 by brett.cannon.

Files
File name Uploaded Description Edit Remove
empty_import.tgz hauser, 2008-02-12 20:50 simple example of empty import
Messages (7)
msg62333 - (view) Author: (hauser) Date: 2008-02-12 20:50
This construction:

__import__( 'pkg', {}, {}, [''] )

Will cause double initialization of package 'pkg', once with name 'pkg'
and second one with name 'pkg.' (trailing dot). Implementation tries to
import subpackage of 'pkg' with empty name, and imports the same package
twice.

This kind of construction is used as a hacky way to obtain exact module
instead of top-level module in return value. It is a hack, but should
not cause this kind of side effects.
msg64079 - (view) Author: Brett Cannon (brett.cannon) Date: 2008-03-19 18:04
As you said, it's a hack, so supporting an abuse of the API is not
reasonable. You don't have to set the fromlist for the import to work.
And if you are doing it to get the tail module, you can write some
simple code to use getattr() to walk down from the root module to the
one you want.

And I plan to add a much simpler API to the imp module for people to use
directly so that these abuses don't continue.
msg64080 - (view) Author: (hauser) Date: 2008-03-19 18:27
There are quite a few projects that use this solution:
http://google.com/codesearch?hl=en&lr=&q=__import__.*%5C%5B%27%27%5C%5D
. I would change it even if it is a hack, but I understand your point.
msg76461 - (view) Author: Mart Sõmermaa (mrts) Date: 2008-11-26 12:01
Just for reference, the simplest workaround is to use:

modname = "foo.bar.baz.baq"
mod = __import__(modname, {}, {}, [modname.rsplit(".", 1)[-1]])
msg76462 - (view) Author: Mart Sõmermaa (mrts) Date: 2008-11-26 12:02
See also http://bugs.python.org/issue4438
msg80987 - (view) Author: Mart Sõmermaa (mrts) Date: 2009-02-02 20:01
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 ...>
msg80989 - (view) Author: Brett Cannon (brett.cannon) Date: 2009-02-02 20:10
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__.
History
Date User Action Args
2009-02-02 20:10:56brett.cannonsetmessages: + msg80989
2009-02-02 20:01:30mrtssetmessages: + msg80987
2008-11-26 12:02:04mrtssetmessages: + msg76462
2008-11-26 12:01:05mrtssetnosy: + mrts
messages: + msg76461
2008-03-19 18:27:34hausersetmessages: + msg64080
versions: + Python 2.5
2008-03-19 18:04:56brett.cannonsetstatus: open -> closed
resolution: invalid
messages: + msg64079
2008-03-18 19:52:28jafosetpriority: normal
assignee: brett.cannon
nosy: + brett.cannon
2008-02-12 20:50:35hausercreate