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

Claudio Grondi claudio.grondi at freenet.de
Mon Mar 7 11:52:13 CET 2005


STeVe,

may I ask you for more details on this?
Any disadvantages while using exec()
in this context?

I try to avoid using any of the
__xxxx__() functions if possible
(considering this a good
programming style).

Claudio

"Steven Bethard" <steven.bethard at gmail.com> schrieb im Newsbeitrag
news:NfKdnYQAU6NcmLHfRVn-oQ at comcast.com...
> Claudio Grondi wrote:
> > "Steven Reddie" <smr at essemer.com.au> schrieb im Newsbeitrag
> > news:f93791bd.0503062134.312a065e at posting.google.com...
> >>
> >>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?
> >
> > modulename = 'module'
> > cmd = 'import '+modulename
> > exec(cmd)
> >
> > Check also the thread:
> >   How do I import everything in a subdir?
> > in THIS newsgroup.
>
> And note that it tells you to use __import__, not exec. =)
>
> STeVe





More information about the Python-list mailing list