Import a lot of modules, or, import 'foo'

Chad Netzer chad at vision.arc.nasa.gov
Sat Jul 31 00:23:48 EDT 1999


"David N. Welton" wrote:

> "Emile van Sebille" <emile at fenx.com> writes:
>
> > This seems to work for me:
> >
> > >>> for t in tests:
> >  exec ('import %s' % t)
>
> Eewwwww, that's kind of twisted, but I guess if it works:-)
>

I do a similar thing occasionally, and the big problem is that it makes
the Traceback system
work poorly, so that tracking down where unexpected exceptions have
occurred during
debugging can be a pain (especially Tkinter or Pmw generated exceptions,
etc.).

But seriously, you should use the __import__ command instead. ie.:

    string = __import__("string")

which is similar (equivalent at the module level?) to:

    import string

To import names from within the module (ala. "from spam_module import
eggs"):

    globals()[name] = getattr(__import__(module_name), name)

Chad






More information about the Python-list mailing list