Newbie question.. how to load modules with a dynamic name?

Gerhard Häring gerhard.nospam at bigfoot.de
Fri Feb 23 18:45:03 EST 2001


Ron Scinta wrote:
> 
> > >  This does not work alas....
> > >  >>> mymod = "ptw"
> >
> > >>> exec('import ' + mymod)
> >
> > ds
> 
> OK, but how then do I reference the dynamic module by name?

In Python 2.0, you can use the "import ... as ..." construct.

for example:

modules2test = [ 'module1', 'module2' ]

for mymodule in modules2test:
    exec( 'import ' + mymodule + ' as test_module' )
    test_module.test()


Gerhard
-- 
Sorry for the fake email, please use the real one below to reply.
contact: g e r h a r d @ b i g f o o t . d e
web:     http://highqualdev.com



More information about the Python-list mailing list