In python docs on __import__ :
__import__( name[, globals[, locals[, fromlist[, level]]]])
...
If the name variable is of the form package.module, it is usually a top-level package (name to the first dot), and not a module by name. However, when a non-empty fromlist argument is given, a module with a name by name is returned. This is done for compatibility with the bytecode generated for various types of import applications; when using "import spam.ham.eggs", the top-level package spam should be placed in the namespace import, but when using "from spam.ham import eggs", the spam.ham subfolder should be used to find variable eggs. As a workaround for this behavior, use getattr () to retrieve the desired component. For example, you could define the following helper:
def my_import(name): mod = __import__(name) components = name.split('.') for comp in components[1:]: mod = getattr(mod, comp) return mod
To rephrase:
When you request somepackage.somemodule , __import__ returns somepackage.__init__.py , which is often empty.
It will return somemodule if you provide fromlist (a list of variable names inside somemodule that you want that are not actually returned)
You can also use the feature they offer.
Note. I asked this question, fully intending to answer it myself. There was a big mistake in my code, and having mistakenly indicated it, it took me a long time to figure it out, so I decided that I would help the SO community and publish the information received here.
dwestbrook Oct 17 '08 at 4:46 2008-10-17 04:46
source share