Python has a variety of packages, so what rules does the Python interpreter use to look for packages?
1. Python will first look for a built-in (BUILT_IN) package
2. Then search in order according to the path specified in the PYTHONPATH environment variable.
3. Then search by the path specified in the. pth file that can be identified by the Python interpreter.
For the next two points, you can view it through Sys.path (the interpreter will create the Sys.path through site.py at startup), as shown in the following illustration:
Sys.path[0] is the relative path (highest priority) of the current directory
SYS.PATH[1] is the directory where the Ipython executable program resides
SYS.PATH[2] is the path specified by Pythonpath
SYS.PATH[3] is the absolute path to the current directory (if Sys.path is printed with-C, this is not the case)
SYS.PATH[10] is determined by the environment in which the Ipython interpreter is located, and it will look for the Site-packages directory in the environment, possibly in the interpreter's built-in.
The remaining path is basically the path specified by the. pth file that the interpreter can find. It is usually sys.path[10] the. pth file in the Site-packages directory.
PS, in fact, can be seen from the figure, package search path mixed with Ipython own "Sihuo", if used python-c "import sys, pprint;" Pprint.pprint (Sys.path) "Print sys.path words, surely there is no sys.path[1], [11], [12] these Ipython Sihuo. As for sys.path[3] this, the Python interpreter is not available either with the-C print Sys.path.
References: http://wecatch.me/blog/2016/05/28/python-module-path-find/