Python Search Package Rules __python

Source: Internet
Author: User
Tags pprint

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.