Possible to import a module whose name is contained in a variable?
Steven Bethard
steven.bethard at gmail.com
Mon Mar 7 02:41:02 EST 2005
Steven Reddie wrote:
> MODULES = [ 'module1', 'module2' ]
>
> def libinfo():
> for m in MODULES:
> __import__('libinfo.'+m)
> m.libinfo()
> CFLAGS+=m.CFLAGS
Does this do what you want?
---------- libinfo/__init__.py ----------
-----------------------------------------
---------- libinfo/module1.py ----------
def libinfo():
return "module1"
----------------------------------------
---------- libinfo/module2.py ----------
def libinfo():
return "module2"
----------------------------------------
py> modules = ['module1', 'module2']
py> libinfo = __import__('libinfo', globals(), locals(), modules)
py> for module in modules:
... getattr(libinfo, module).libinfo()
...
'module1'
'module2'
STeVe
More information about the Python-list
mailing list