Skip to content Skip to sidebar Skip to footer

Problem With Python And __import__

Sorry for the generic title, will change it once I understand the source of my problem I have the following structure: foo/ foo/__init__.py foo/bar/ foo/bar/__init__.py foo/bar/som

Solution 1:

I believe the proper way to do this is:

mod = __import__('foo.bar', fromlist = ['some_module'])

This way even the 'foo.bar' part can be changed at runtime. As a result some_modulewill be available as mod.some_module; use getattr if you want it in a separate variable:

the_module = getattr(mod, 'some_module')

Solution 2:

from foo.barimport *

is a bad practice since it imports some_module into the global scope.

You should be able to access your module through:

import foo.barmod= getattr(foo.bar, 'some_module')

It can be easily demonstrated that this approach works:

>>>import os.path>>>getattr(os.path, 'basename')
<function basename at 0x00BBA468>
>>>getattr(os.path, 'basename\n')
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    getattr(os.path, 'basename\n')
AttributeError: 'module' object has no attribute 'basename
'

P.S. If you're still interested in using your kind of import statement. You need an eval:

from foo.barimport *
eval('some_module')

To clarify: not only it's bad practice to use *-import it's even worse in combination with eval. So just use getattr, it's designed exactly for situations like yours.

Solution 3:

From the docs:

Direct use of __import__() is rare, except in cases where you want to import a module whose name is only known at runtime.

However, the dotted notation should work:

mod = __import__('foo.bar.some_module')

Post a Comment for "Problem With Python And __import__"