This is a Python bug. Passing the null string as an element of fromlist
is illegal, and should raise an exception.
There's no need to include ""
in fromlist
; that's implicit--the module itself is always loaded. What's actually happening is the module.submodule
string is using the null string, resulting in the module name testpkg.
, with a trailing period. That gets imported literally, and since it has a different name than testpkg
, it's imported as a separate module.
Try this:
pkg = __import__("testpkg", fromlist=[''])
import sys
print sys["testpkg"]
print sys["testpkg."]
... and you'll see the duplicate module.
Someone should probably file a ticket on this if there isn't already one; too tired to do it myself right now.