from spam import eggs, spam at runtime, how?

Just just at xs4all.nl
Tue Dec 9 16:32:35 EST 2003


In article <3FD63B25.DC412905 at engcorp.com>,
 Peter Hansen <peter at engcorp.com> wrote:

> Paul Rubin wrote:
> > 
> > Rene Pijlman <reply.in.the.newsgroup at my.address.is.invalid> writes:
> > > >Why not just stick the import statement where you need it?
> > >
> > > Because I need a module from one of a number of different packages, at one
> > > specific point in my code.
> > 
> > I still don't understand why you don't want to use an exec statement
> > for this.  That's by far the most natural and understandable way to do it.
> 
> In this case, he's getting the actual string from the web, so I wouldn't
> be surprised if exec would be a real can of security worms.

Also, __import__ returns a module object, so you don't have to pull it 
out of the globals you passed to exec. Compare:

   g = {}
   exec "import %s" % modulename in d
   m = g[modulename]

with

   m = __import__(modulename)

Just




More information about the Python-list mailing list