__import__(x) VS __import__(x, {}, {}, [''])

greg greg at cosc.canterbury.ac.nz
Mon Aug 24 04:34:41 CEST 2009

Phil wrote:
> I am trying to understand the difference between __import__(x) and
> __import__(x, {}, {}, ['']).
> The documentations wording was a bit weird for me to understand:
> "The standard implementation does not use its locals argument at all,
> and uses its globals only to determine the package context of the
> import statement."
 > Does that mean it passes in the globals by default, or does the above
 > statement stand true only if globals() is passed in?

Not sure what you mean by "it" in that question.

This is talking about the built-in implementation of
the __import__ function, and what it does with the
'globals' and 'locals' arguments that get passed to
it (by whatever means).

If you call it without a value for 'globals', it uses
the current value of globals().

The bytecode generated for the 'import' statement
always calls it with the current globals() and locals().

If you pass an empty dict for 'globals', I think the
effect will be to cause the module name to always be
interpreted as a top-level module (i.e. no relative
import). But I'm not sure.


More information about the Python-list mailing list