What are the `globals` and` locals` parameters in the Python __import__ function?

The Python documentation has a __import__ part that I don't understand:

__import__(name[, globals[, locals[, fromlist[, level]]]])

The function imports the name module, potentially using the globals and locals data to determine how to interpret name in the package context. The standard implementation does not use its locals argument at all and uses its globals only to determine the context of the import statement package.

What does it mean to “interpret” a module name? What is a package context?

An example call using these parameters is as follows:

 spam = __import__('spam', globals(), locals(), [], -1) 

Why does this example provide globals() and locals() functions? What happens when I provide only globals() ? Or not?

I probably missed some of the namespace logic regarding importing modules. Could you point me to an article that explains this / has examples with the __import__ function?

+8
python import
source share
3 answers

The standard implementation does not use its locals argument at all and uses its globals only to determine the context of the import statement package.

(from docs.python.org )

I still don't know how to use globals ; What global variable can affect the import statement?

EDIT:. Looking at import.c in a Python 2.5 source, I found that __import__ expects to find either __name__ or __path__ in globals to increase the import search path relative to the path (s) found in one of these variables, in that order.

+4
source share

globals used to determine the current context on which the import is invoked. For example:

 """ /myproject/a/b.py /myproject/a/foo.py /myproject/c/d.py /myproject/c/foo.py """ # Which foo gets imported? import foo #1 foo = __import__('foo') #2 

They do not match, since there is no (simple) way on # 2 to find out which module the import is called from. The __import__ function needs to know which current module is actually importing the correct foo .

Internally on __import__() , globals used to get a reference to the current module that is causing the import. From __import__ source code :

Return the package to which you are importing. If global variables from the foo.bar.bat module (not the package itself) appear, this returns sys.modules for foo.bar. If globals is from an init .py package, the package entry in sys.modules is returned as a borrowed reference.

+1
source share

What does it mean to “interpret” a module name? What is a package context?

When entering

 >>> a 

Python must "interpret" this name. Is it global? Is it local?

 >>> def f(x): ... return x * a 

Now x explicitly local. a must be "interpreted". Global? Local

Why does this example provide globals () and locals () functions for this function? What happens when I provide only global ()? Or not?

Try and see. Jokes aside. It’s easier to play with than to ask.

What matters is what you do in the prompt >>> globally.

You will need to define the functions that will create the local context so that you can see the differences between global and local.

0
source share

All Articles