Why can't you refer to modules that are automatically loaded by the interpreter without the additional `import` statement?

When you start your Python interpreter, it seems that some modules / packages are automatically imported during the startup process:

python Python 2.7.6 (default, Jan 13 2014, 14:59:37) ... >>> import sys >>> for key in sys.modules.iterkeys(): ... print(key) ... os sys abc others ... 

However, these modules seem to have been loaded into another namespace / space, because you cannot access them without additional import :

 >>> abc Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'abc' is not defined 

Here are my questions:

  • What exactly loads these modules and for what purpose?
  • What namespace / space was loaded in?
+6
source share
3 answers

When starting a new interpreter, sys.modules will contain those modules that were automatically loaded by Python, because they fulfill the roles required for the Python interpreter to run. For example, Py_InitializeEx in Python/pythonrun.c includes strings such as

 bimod = _PyBuiltin_Init(); 

which initializes the __builtin__ module, where built-in such as int and open live, and

 sysmod = _PySys_Init(); 

which initializes the sys module.

As for the namespace into which these modules are loaded, the modules do not actually load in the namespaces. The import statement loads the module and assigns the module or elements from the module to a variable or variables in the current namespace, but loading the module and assigning are independent steps. Calls of type _PySys_Init do not assign a module to any particular namespace. However, the imported technique will write to sys.modules any module loaded with any code in the current Python execution, execute the module and create a new copy.

+2
source

the sys module loads them into the sys.modules dictionary sys.modules

if you want me to be able to access them as

 abc = sys.modules["abc"] 

but not sure why you want

+4
source

It includes all the modules that you have ever imported, so if you are del module , the module will still be displayed in the dict, but that does not mean that you can access it:

 In [16]: import BeautifulSoup In [17]: sys.modules["BeautifulSoup"] Out[17]: <module 'BeautifulSoup' from '/usr/local/lib/python2.7/dist-packages/BeautifulSoup.pyc'> In [18]: BeautifulSoup.re Out[18]: <module 're' from '/usr/lib/python2.7/re.pyc'> In [19]: del BeautifulSoup In [20]: sys.modules["BeautifulSoup"] Out[20]: <module 'BeautifulSoup' from '/usr/local/lib/python2.7/dist-packages/BeautifulSoup.pyc'> In [21]: BeautifulSoup.re --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-21-db67e3f66def> in <module>() ----> 1 BeautifulSoup.re NameError: name 'BeautifulSoup' is not defined 

Python preloads some module at startup, and any imported modules that also import other modules will mean that other modules may appear in sys.modules, but are not available in your namespace:

 In [1]: sys.modules["numpy"] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-1-88fb63c48e78> in <module>() ----> 1 sys.modules["numpy"] KeyError: 'numpy' In [2]: cat test.py import numpy In [3]: import test In [4]: sys.modules["numpy"] Out[4]: <module 'numpy' from '/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'> In [5]: numpy.array --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-132169fc46d3> in <module>() ----> 1 numpy.array 
+3
source

Source: https://habr.com/ru/post/988121/


All Articles