on package import, have it conditionally import a subpackage

Gabriel Rossetti gabriel.rossetti at arimaz.com
Sat Sep 19 16:06:11 EDT 2009


Hello everyone,

I'd like to ba able to import a package and have it's __init__ 
conditionally import a subpackage. Suppose that you have this structure :

mybase/
mybase/__init__.py
mybase/mypkg
mybase/mypkg/__init__.py
mybase/mypkg/module0.py
mybase/mypkg/type1
mybase/mypkg/type1/__init__.py
mybase/mypkg/type1/module1.py
mybase/mypkg/type1/module2.py
mybase/mypkg/type2
mybase/ mypkg/type2/__init__.py
mybase/ mypkg/type2/module1.py
mybase/ mypkg/type2/module2.py

I'd like to do something like th following in my code

 >>> import mybase
 >>> mybase.setType("type1")
 >>> from mybase.mypkg import module0, module1, module2
 >>> module0.__file__
'mybase/mypkg/module0.pyc'
 >>> module1.__file__
'mybase/mypkg/type1/module1.pyc'
 >>> module2.__file__
'mybase/mypkg/type1/module2.pyc'

but I can't figure out how to do this.

Thank you,
Gabriel




More information about the Python-list mailing list