Possible to import a module whose name is contained in a variable?

Claudio Grondi claudio.grondi at freenet.de
Mon Mar 7 03:21:28 EST 2005


"Steven Reddie" <smr at essemer.com.au> schrieb im Newsbeitrag
news:f93791bd.0503062134.312a065e at posting.google.com...
> Hi,
>
> I want to do something like the following, which doesn't work:
>
>     modulename = 'module'
>     import modulename
>
> The error is that there is no module named 'modulename'.  Is there a
> way to get that variable expanded?
>
> Regards,
>
> Steven

modulename = 'module'
cmd = 'import '+modulename
exec(cmd)

Check also the thread:
  How do I import everything in a subdir?
in THIS newsgroup.

Claudio
P.S.
> MODULES = [ 'module1', 'module2' ]
>
> def libinfo():
> for m in MODULES:
> __import__('libinfo.'+m)
> m.libinfo()
> CFLAGS+=m.CFLAGS

indentation error?






More information about the Python-list mailing list