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

Alex Martelli aleaxit at yahoo.com
Sat Feb 24 04:31:40 EST 2001


"Ron Scinta" <nb0klun71q5001 at sneakemail.com> wrote in message
news:FUzl6.227$Gm2.5865 at skycache.prestige.net...
> > >  This does not work alas....
> > >  >>> mymod = "ptw"
> >
> > >>> exec('import ' + mymod)
> >
> > ds
>
> OK, but how then do I reference the dynamic module by name?
>
> mymod. (The period does not help here, still thinks I am using the string
> class instead of my imported module)....
>
>  Is there a way to alias the module name at this point?

sys.modules[mymod] will refer to the module object (you'll need to
import sys, of course).  You can assign that to whatever name you
like, e.g.,

    floop = sys.modules[mymod]
    floop.pleep('praak')


Alex






More information about the Python-list mailing list