__import__ confusion

Justin Ng justin.ng at NOSPAMdspfactory.com
Tue Feb 25 13:43:54 EST 2003


On Tue, 25 Feb 2003 00:41:04 -0300, Leazen <leazen at uol.com.ar> wrote:

> 	I'm having some trouble understanding how __import__ works.
> 	From the docs I read that "from spam.ham import eggs" results in
> "__import__('spam.ham', globals(), locals(), ['eggs'])" rendering the
> later function call 'eggs', what ever that is, but I actually get 'ham'!

It seems to be that in the particular example you're talking about, the 
docs state:

"Note that even though locals() and ['eggs'] are passed in as arguments, 
the __import__() function does not set the local variable named eggs; this 
is done by subsequent code that is generated for the import statement."

The implication from the docs seems to be that __import__ does not mimic 
the import statement, but the import statement uses __import__ and 
additional code to perform what you expect of it.

Justin Ng




More information about the Python-list mailing list