Detailed explanation of the use of module search in Python

黄舟
Release: 2017-08-11 14:06:53
Original
1336 people have browsed it

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
Copy after login

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
Copy after login

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])
&#39;/Users/x/workspace/blog-code&#39;
>>>
Copy after login

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
Copy after login

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 &#39;__file__&#39; is not defined
Copy after login

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]
Copy after login

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]
Copy after login

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

Since Python searches for modules in

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[&#39;tornado&#39;]
Traceback (most recent call last):
 File "<input>", line 1, in <module>
KeyError: &#39;tornado&#39;
>>> import tornado
>>> sys.modules[&#39;tornado&#39;]
<module &#39;tornado&#39; from &#39;/Users/x/python_dev/lib/python2.7/site-packages/tornado/__init__.pyc&#39;>
Copy after login

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[&#39;os&#39;]
<module &#39;os&#39; from &#39;/Users/x/python_dev/lib/python2.7/os.pyc&#39;>
>>>
Copy after login

借助 sys.modules 和 __file__,可以动态获取所有已加载模块目录和路径。


>>> import os
>>> os.path.realpath(sys.modules[&#39;os&#39;].__file__)
&#39;/Users/x/python_dev/lib/python2.7/os.pyc&#39;
>>> import tornado
>>> os.path.realpath(sys.modules[&#39;tornado&#39;].__file__)
&#39;/Users/x/python_dev/lib/python2.7/site-packages/tornado/__init__.pyc&#39;
Copy after login


def get_module_dir(name):
 path = getattr(sys.modules[name], &#39;__file__&#39;, None)
 if not path
 raise AttributeError(&#39;module %s has not attribute __file__&#39;%name)
 return os.path.dirname(os.path.abspath(path))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template