简体   繁体   中英

Python import iteration, sys.path

So I was wondering, at this point in time I'm reading a book about Python. The book explains the following:

The import algorithm

To truly understand namespace packages, we have to look under the hood to see how the import operation works in 3.3. During imports, Python still iterates over each directory in the module search path, sys.path , just as in 3.2 and earlier.

My question is: How is python able to iterate through sys.path when sys is not imported. Also if python is able to see sys without import to iterate through sys.path why do we need to import sys in our code?

>>> sys
NameError: name 'sys' is not defined.

>>> import sys
>>> sys
<module 'sys' (built-in)>

There's no contradiction. Python's sys module exposes the search path configurations that modifies the behaviour of import to the Python side, but even without importing sys in your Python code, the interpreter knows about its own configurations.

In the following CPython source code it is commented that

/* _PyMem_SetDefaultAllocator() is needed to get a known memory allocator,
   since Py_SetPath(), Py_SetPythonHome() and Py_SetProgramName() can be
   called before Py_Initialize() which can changes the memory allocator. */

What this means is that Py_SetPath() , which is responsible for setting the module search path, can be executed so early, before any Python code can be interpreted (for example, import statements), that it needs its own memory allocator before the interpreter's own memory allocator takes over.

By the time Python interpreter's main() function is run,it can already read the path configuration using Py_GetPath() that calls the internal function _PyPathConfig_Init() if necessary, which is safe to do even before the interpreter is ready to execute Python code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM