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/some_module.py
When I try to import some_module by doing so:
from foo.bar import some_module
it works like a charm. But this is no good for me, since I only know the name of the module to import in runtime. so if I try:
from foo.bar import *
mod=__import__('some_module')
I get an error. Am I doing something wrong? Is there a better way to do this? and why is this happening?
Why is that? I am not quite sure I completely understand the concept behind python packages. I thought they were equivalent to java's packages and thus
I believe the proper way to do this is:
This way even the 'foo.bar' part can be changed at runtime. As a result
some_module
will be available asmod.some_module
; use getattr if you want it in a separate variable:From the docs:
However, the dotted notation should work:
is a bad practice since it imports
some_module
into the global scope.You should be able to access your module through:
It can be easily demonstrated that this approach works:
P.S. If you're still interested in using your kind of import statement. You need an
eval
:To clarify: not only it's bad practice to use
*
-import it's even worse in combination witheval
. So just usegetattr
, it's designed exactly for situations like yours.