current position：Home>Analysis of the principle of Python import
Analysis of the principle of Python import
One 、 brief introduction
Python code in one module gains access to the code in another module by the process of importing it.
Simply speaking , What we see everyday
.py file , Is called a
When your python The code needs to get some external functions （ Some wheels have been built ）, You need to use
import This declaration keyword .
import Can help import other module .（ similar C An appointment
import Declaration is a common import method , But it's not the only way . That is, it can be done in other ways module Import .
import Statement combines two operations ：
- Search for named modules . Call... By passing in the appropriate parameters
- Bind the search results to the local namespace .
__import()__The return value of is used as the namespace binding operation .
import Statement execution time ,
__import__() Will be called ,Python I'll look for module And create a module object And initialize it ; If module Did not find , Will throw out
ModuleNotFoundError One of the exception .
Simply speaking , call
__import__() It's just
import Declare a subset of operations .
Call directly import() Perform module search only , If you find , Execute the module creation operation . Although there may be some side effects , For example, import parent package , And updating various caches （ Include sys.modules）, But only import Statement to perform name binding operation .
Two 、 lookup Module The way
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:The directory containing the input script (or the current directory when no file is specified). PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH). The installation-dependent default.
import Execution time , Will try to find... In the following order module：
- The parser first tries to search for its own built-in module
- If you can't find it , Will be based on
sys.pathOrder lookup for
pyThe folder where the execution file itself is located ;
PYTHONPATHenvironment variable ;
- python The default installation depends on the location
You can view through the following operations sys.path The path of
$ python3 Python 3.5.2 (default, Jan 26 2021, 13:30:48) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path ['', '/home/tester/opt/18.104.22.168', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
therefore , If you need to use some third-party libraries , In addition to pip Call directly after installation , Also can put the module Put it in the corresponding directory , And then use
PYTHONPATH Specify the directory .
3、 ... and 、import Lead in deep exploration
- Import requires module Complete path ,Python Will try to import from top to bottom . When python Try importing
foo.bar.bazwhen , Will try to import
foo.bar.baz, If any intermediate import fails , Will trigger
- Import yes cache The concept of . Each import will try to
sys.modulesThis cache Search for , If you return None It will be thrown out.
ModuleNotFoundErrorerror , If module name Can't find ,Python Will try to keep looking down
- import The latest underlying mechanism of , It's through
loadersCombine the two to find module And import ,
findersResponsible for finding relevant paths ,
loadersResponsible for loading .
actual module Lookup order
sys.meta_pathhas three meta path finders, one that knows how to import built-in modules, one that knows how to import frozen modules, and one that knows how to import modules from an import path (i.e. the path based finder).
As can be seen from the above ,python import It will follow a certain search order . Except for the second chapter 3 A path , There is another layer in the actual front cache
sys.modules,The module cache.
sys.meta_path: Generally speaking, there are 3 individual finders
- one that knows how to import built-in modules
- one that knows how to import frozen modules
- one that knows how to import modules from an import path
this 3 individual finders The corresponding path , Just as the second chapter above said 3 A search path
Can pass Python Interactive command to view
[email protected][SJC]~$ python3 Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> print(sys.meta_path) [<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>]
Four 、 Relative Import and absolute import
Relative Import , With
. Lead , Follow linux File systems are similar to ,
.. They represent the present package And the parent package
Absolutely import , It's usually
import <> or
from <> import <> This form
for example ：
package The directory structure is as follows
package/ __init__.py subpackage1/ __init__.py moduleX.py moduleY.py subpackage2/ __init__.py moduleZ.py moduleA.py
The following are relative imports
from .moduleY import spam from .moduleY import spam as ham from . import moduleY from ..subpackage1 import moduleY from ..subpackage2.moduleZ import eggs from ..moduleA import foo
Here is the absolute import
import package.subpackage1.moduleX from package.subpackage1 import moduleX
5、 ... and 、 Extension
Generally we use import Import module when , What principles should be followed ,PEP8 The following suggestions are given ：
Imports should be grouped in the following order:Standard library imports. Related third party imports. Local application/library specific imports. You should put a blank line between each group of imports.
import Organizational order ：
- Standard library import
- Third party Library import
- Local application or library import
Python The new version provides a api Can be controlled import The rules of , Avoid changing directly in the old way
__import__() Complicated operation , And reduce the concept of errors .
importlib Modules provide a wealth of API To interact with the import system . for example
importlib.import_module() Provides a recommended 、 Than built-in
__import__() Simpler API To invoke the import mechanism .
sys.modules Will be in module import Update when done cache, For next import Quick access .
sys.modules, The module cache This mapping serves as a cache of all modules that have been previously imported, including the intermediate paths. So if foo.bar.baz was previously imported, sys.modules will contain entries for foo, foo.bar, and foo.bar.baz. Each key will have as its value the corresponding module object.
finder My job is to search , Follow loader Separation of work
A finder’s job is to determine whether it can find the named module using whatever strategy it knows about.
Search path , It's not just
sys.path All the paths , some subpackages Your search may depend on parent package Of
import path: A list of locations (or path entries) that are searched by the path based finder for modules to import. During import, this list of locations usually comes from
sys.path, but for subpackages it may also come from the parent package’s
import The mechanism is extensible , Detailed view
Import hooks The concept . There are two main import hooks: meta hooks and import path hooks
The import machinery is designed to be extensible; the primary mechanism for this are the import hooks. There are two types of import hooks: meta hooks and import path hooks.
3 Default finder, Search for different policies module
Python’s default sys.meta_path has three meta path finders, one that knows how to import built-in modules, one that knows how to import frozen modules, and one that knows how to import modules from an import path (i.e. the path based finder).
author[zero000],Please bring the original link to reprint, thank you.
The sidebar is recommended
- Python simulates keyboard input and mouse operation
- Python get windows special folder path code
- Python segmented download file
- Python setting environment variables for processes
- Python uses multithreading to execute CMD command to shut down
- Python gets the number of days in the previous month
- Python generates 128 barcode (code128)
- [Python] code sharing for drawing 2D scatter diagram
- [Python] output the names of students with the highest or lowest scores and students with lower than average scores
- [Python] implement the maximum and minimum distance algorithm
guess what you like
Python parsing JSON data tutorial
Deep learning project: how to use Python and opencv for face recognition
Pychart developing Django project template common filter tutorial
Using Python to call cloud API to monitor the traffic usage of lightweight application servers
Face core app access - server Python demo
Can Python's "King" status be maintained in the next decade?
How do Python crawlers make money? Six Python crawlers make money. It's not a problem to engage in sidelines
Java or python, which is more suitable for AI development?
How to make beeps in Python for windows ECS
Three sorts (select, bubble, insert) Python version
- Python automatic switching environment
- Detailed explanation of python3 rounding problem
- [Master Wu's Python bakery] day 2
- [Master Wu's Python bakery] day 1
- [Master Wu's Python bakery] day 3
- [Master Wu's Python bakery] day 4
- [Master Wu's Python bakery] day 5
- [Master Wu's Python bakery] day 6
- [Master Wu's Python bakery] day 7
- [Master Wu's Python bakery] day 8
- Introduction and examples of socket programming in Python
- Python notes - permissionerror
- Python notes - deprecationwarning
- Python notes - Open Python project
- Python notes - PIL Library
- Python notes - with as statement
- How to export IPython history to Py file?
- Python multithreading combined with dataloader to load data
- Make Python not echo commands that get password input
- In c/c++ and python programming, null and none cannot be distinguished clearly
- Writing sample code for functions in Python
- Summary of operation methods of Python set (about 20 operation methods), with sample code attached
- Python -- functions
- Anonymous and recursive functions in Python
- How to query the methods (member functions) of a class or an object in Python [using the function dir()]
- Summary of operation methods of Python Dictionary (dict) (about 18 operation methods), with sample code attached
- Collect hot search lists in Python at work, which can be called a fishing artifact
- Running Django and Vue on docker
- Data classification in pandas
- About Python: docxtpl is embedded by default when inserting pictures
- How to solve the problem of CSV? (Language Python)
- Installation and use of redis (Python)
- Python implements sending mail (implements single / group mail verification code)
- On the built-in object type of Python -- number (one of the differences between py2 and PY3)
- Python God uses a problem to help you solve the problems of formal and actual parameters in Python functions
- "Project Euler Python Mini tutorial" 001-010 solution introduction
- Most beginners learn Python and web automation. In this way, they learn and give up
- Python matrices and numpy arrays
- Exciting challenge: Python crawler crawls the cover picture of station B
- After installing python3, use the yum command to report an error?