search
HomeBackend DevelopmentPython TutorialIntroduction to the principles and methods of python module search

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

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 &#39;__file__&#39; 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

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;>

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;>
>>>

借助 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;


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))

summary

总的来说,Python 是通过查找 sys.path 来决定包的导入,并且系统包优先级>同目录>sys.path,Python 中的特有属性 __file__ 以及 sys.argv[0]sys.modules 都能帮助我们理解包的查找和导入概念,只要能正确理解 sys.path 的作用和行为,理解包的查找就不是难题了。

The above is the detailed content of Introduction to the principles and methods of python module search. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft