dynamic import

John Roth newsgroups at jhrothjr.com
Sat Jul 24 08:29:16 EDT 2004


<510046470588-0001 at t-online.de> wrote in message
news:87u0vyxwg3.fsf at debian.i-did-not-set--mail-host-address--so-shoot-me...
> "John Roth" <newsgroups at jhrothjr.com> writes:
> >
> > the import statement unfortunately doesn't take a string at
> > run time. As Duncan says, you need to use the
> > __import__() builtin. Read up on it in the library
> > reference (it's the very first entry in the builtins section,)
> > because there are some considerations in using it that
> > aren't exactly obvious on first glance.
> >
> > The other alternative is something like:
> >
> > exec "import " + f[0]
> >
> > This will work as well, and it might be a bit clearer, depending
> > on what you're trying to do.
>
> if one calls the __import__ function with the same argument several times,
> will the module be cached inbetween?

You should get the same copy of the module.

John Roth
>
> Klaus Schilling





More information about the Python-list mailing list