dynamic module import?

alex23 wuwei23 at gmail.com
Sun Jan 18 12:55:55 CET 2009

On Jan 17, 3:55 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> Both very good points, but consider that you're not comparing apples with
> apples.
> >>> __import__("os", fromlist=["system"])
> <module 'os' from '/usr/lib/python2.5/os.pyc'
> >>> system
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'system' is not defined

I must confess I've rarely had a need to use __import__ and don't
think I've ever used the fromlist arg. I'm confused, though, because
the docstring states:

 The fromlist should be a list of names to emulate ``from name
import ...''

But it also states that __import__ always returns a module, so I'm
utterly confused as to the purpose of fromlist, or how to inject the
specified entries into the calling namespace. If anyone could explain
this for me, I'd really appreciate it.

> I mention this only to be pedantic, because I agree with your point that
> exec can introduce security issues, and be significantly slower.

Correcting misinformation isn't pedantry, especially when I've learned
something :)

More information about the Python-list mailing list