Home > Article > Backend Development > Detailed explanation of the use of module search in Python
This article mainly introduces you to the principles and methods of module search in Python. The article introduces it in great detail through example code. It has certain reference learning value for everyone's study or work. Friends who need it can follow the editor below. Come and learn together.
Preface
This article mainly introduces to you the principles and methods of python module search, and shares it for your reference and study. The following is not Say more, let’s take a look at the detailed introduction:
Basic concepts
module
Module, a py file or other file that can be imported is a module
package
Package, a file containing an __init__ file Folder
relative path
Relative path, a path relative to a directory
absolute path
Absolute path, full path
Path search
The python interpreter searches for imported packages or modules
How the Python interpreter searches for packages and modules
When Python executes a py file, whether the execution method is an absolute path or a relative path, the interpreter will add the directory where the file is locatedsys.path
In this list, Python searches for packages and modules in sys.path
, and the content in sys.path
itself is the Python environment. Variables determine.
code-1
#test.py import os import sys print sys.path[0] # execute python test.py python /Users/x/workspace/blog-code/p2016_05_28_python_path_find/test.py
Execution shows that both relative paths and absolute paths output the same results, and no matter which execution method, test.py is located The folders will be added to the top of sys.path
, which is the position with index 0.
What is the order in which the Python interpreter searches for packages?
The interpreter searches for packages first, searching for the built-in module first, and then searching sys.path
, this search sequence will cause the package or module with the same name to be obscured.
code-2
#ls ├── os.py ├── test2.py ├── redis.py #test2.py import os from redis import Redis #execute test2.py Traceback (most recent call last): File "/Users/x/workspace/blog-code/p2016_05_28_python_path_find/test2.py", line 1, in <module> from redis import Redis ImportError: cannot import name Redis
Since os is a built-in module, even if there is a module with the same name in the same directory, the interpreter can still find the correct os module, it can be confirmed that the built-in module will not be blocked, and redis is a third-party module. The default installation location is site-packages in the Python environment variable. After the interpreter is started, the contents of this directory will be added to sys. path
, since the current directory will be at the first place of sys.path
, the redis of the current directory will be found first, and the redis module in site-packages will be obscured.
Search sequence for interactive execution environment
Enter the interactive execution environment, the interpreter will automatically add the current directorysys.path
, then the current directory appears in the form of a relative path in sys.path
:
>>> import os.path >>> import sys >>> os.path.abspath(sys.path[0]) '/Users/x/workspace/blog-code' >>>
other than Except for that, it's the same as executing a file.
__file__ variable in the module
__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. If a module is loaded from a file, __file__ is the path name of the module –Python Doc:
As the name suggests, when the module appears as a file, __file__ refers to the module file The path name, __file__ executed as a relative path is a relative path, and __file__ executed as an absolute path is an absolute path.
#test3.py print __file__ #相对路径执行 python test3.py test3.py #绝对路径执行 python /Users/x/workspace/blog-code/p2016_05_28_python_path_find/test3.py /Users/x/workspace/blog-code/p2016_05_28_python_path_find/test3.py
In order to ensure that __file__ can accurately get the correct location of the module every time, it is best to take the absolute path to it againos.path.abspath(__file__ )
.
__file__ in interactive shell
>>> __file__ Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name '__file__' is not defined
This is because the execution of the current interactive shell is not loaded in the form of a file, so it does not exist_ _file__ such attributes.
sys.argv[0] Variable
##sys.argv[0] is what it is used for Get the main entry executable file.
#test.py import sys print __file__ print sys.argv[0]The above print outputs the same result, because the main execution file and the module to which __file__ belongs are the same. When we change the entry file, the difference appears.
#test.py import sys print __file__ print sys.argv[0] #test2.py import test #execute test2.py /Users/x/workspace/blog-code/p2016_05_28_python_path_find/child/test.py #__file__ test2.py #sys.argv[0]In general,
sys.argv[0] is the path to get the entry execution file, and __file__ is the path to get any module file.
The role of sys.modules
sys.path , Where are the loaded modules stored? The answer is
sys.modules. Once the module is loaded, Python will add the module to
sys.modules for the next loading. This can speed up the introduction of the module and act as a cache.
>>> import sys >>> sys.modules['tornado'] Traceback (most recent call last): File "<input>", line 1, in <module> KeyError: 'tornado' >>> import tornado >>> sys.modules['tornado'] <module 'tornado' from '/Users/x/python_dev/lib/python2.7/site-packages/tornado/__init__.pyc'>As mentioned before, after the Python interpreter is started, the built-in module will be preloaded, which can be verified through
sys.modules .
>>> sys.modules['os'] <module 'os' from '/Users/x/python_dev/lib/python2.7/os.pyc'> >>>
借助 sys.modules
和 __file__,可以动态获取所有已加载模块目录和路径。
>>> import os >>> os.path.realpath(sys.modules['os'].__file__) '/Users/x/python_dev/lib/python2.7/os.pyc' >>> import tornado >>> os.path.realpath(sys.modules['tornado'].__file__) '/Users/x/python_dev/lib/python2.7/site-packages/tornado/__init__.pyc'
def get_module_dir(name): path = getattr(sys.modules[name], '__file__', None) if not path raise AttributeError('module %s has not attribute __file__'%name) return os.path.dirname(os.path.abspath(path))
summary
总的来说,Python 是通过查找 sys.path
来决定包的导入,并且系统包优先级>同目录>sys.path
,Python 中的特有属性 __file__ 以及 sys.argv[0]
,sys.modules
都能帮助我们理解包的查找和导入概念,只要能正确理解 sys.path
的作用和行为,理解包的查找就不是难题了。
总结
The above is the detailed content of Detailed explanation of the use of module search in Python. For more information, please follow other related articles on the PHP Chinese website!