Cannot import a module from a variable

Tim Williams tim at tdw.net
Tue Oct 17 05:57:34 EDT 2006


On 16/10/06, Bruno Desthuilliers <onurb at xiludom.gro> wrote:
> Jia Lu wrote:
> > Hi all:
> >
> > I try to do things below:
> >>>> import sys
> >>>> for i in sys.modules.keys():
> >       import i
> > Traceback (most recent call last):
> >   File "<pyshell#67>", line 2, in <module>
> >     import i
> > ImportError: No module named i
> >
> > But it seems that import donot know what is i ?
>
> The import statement expects a name (a symbol), not a string.
>

eval( 'import %s' % modname)

and

eval( 'reload(%s)' % modname)

Usual warnings about eval apply,  but in this case it is usable.

HTH :)



More information about the Python-list mailing list