Detailed explanation of the search order of modules imported by python (detailed explanation of the mechanism of python import search module)

reference

Detailed explanation of the module search sequence imported by python (detailed explanation of the mechanism of python import search module): https://blog.csdn.net/qq_27825451/article/details/100552739

import sys
sys.path
'''返回的列表为:
['',   # 运行程序的根目录
 'D:\\ProgramData\\Anaconda3\\envs\\pytorch1.2.0\\python36.zip', # 这几个是python标准库目录
 'D:\\ProgramData\\Anaconda3\\envs\\pytorch1.2.0\\DLLs', 
 'D:\\ProgramData\\Anaconda3\\envs\\pytorch1.2.0\\lib', 
 'D:\\ProgramData\\Anaconda3\\envs\\pytorch1.2.0', 
 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python36\\site-packages', 
 'D:\\ProgramData\\Anaconda3\\envs\\pytorch1.2.0\\lib\\site-packages'  # 最后面这个是第三方模块所在的目录
]
'''
'''
# 注意:这里是没有设置PYTHONPATH 环境变量的路径值的,也没有创建.pth文件
'''

In Python, a built-in module (Built-in Module) refers to a set of modules that come with the Python installation package. These modules provide basic, general-purpose functions and tools that can be widely used in various Python program development , such as string operations, file IO, mathematical calculations, regular expressions, date and time processing, network communications, and more. Built-in modules are loaded into memory when the Python interpreter starts, and can be used directly without installation or import.

The number of Python built-in modules is very large, and most commonly used modules are classified as core modules (commonly used are: math, sys, os, re, json, random, etc.), and there are also some commonly used standard library modules ( Commonly used are: datetime, collections, itertools, argparse, logging, etc.) are standard modules, and these standard modules are all included with the Python installation package.

When you want to use the built-in module, you only need to call it directly in the Python interpreter or in the Python program, without installing or importing it first. For example, to perform mathematical calculations using the built-in module math, you only need to type in Python:

import math

print(math.sqrt(2))

In this example, we use the function mathof the built-in module sqrt()to find the square root of 2. There is no need for other imports in the Python program, just call the math module directly.

Summarize:

The order in which python searches for modules is as follows:

Built-in modules>

The root directory of the program (that is, the directory where the python file is currently running) >

The directory set by the PYTHONPATH environment variable >

Directories of the standard library >

Contents of any *.pth files that can be found>

The site-package directory for third-party extensions

What is the order of importing bread in python code

What is the order of importing packages in python code? If there is a package with the same name in the current directory, should the package in the current directory be imported first, or directly from the virtual environment?

In Python, packages are imported sys.pathin the order of the path list in . sys.pathis the default search path when a Python program is running, including the following paths:

  • The directory where the current script is located
  • PYTHONPATHThe path contained in the operating system environment variable
  • The standard library path under the Python installation path (usually in /usr/lib/python3.7/or /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/)
  • Third-party library paths defined in the Python search path

When you import a package in a Python program, Python will search the package in order according to the above path list until it finds the package version that appears first, and subsequent searches will automatically ignore the package. Therefore, if there is a package with the same name in the current directory, and sys.paththe path appears before the path of the virtual environment in , Python will first search for the package in the current directory and import the modules in it instead of the package in the virtual environment.

If you want to ensure that Python imports the packages in the virtual environment, you can explicitly specify the virtual environment path at the beginning of the program, for example:

import sys
sys.path.insert(0, '/path/to/virtual/env')

In this way, the path of the virtual environment can be added to sys.paththe top of the list to ensure that Python imports the packages in the virtual environment.

Guess you like

Origin blog.csdn.net/qq_15821487/article/details/131536371