Use PTH files to make Python easy to import the modules you write yourself

Source: Internet
Author: User

Transferred from: Http://www.elias.cn/Python/PythonPath

The article is simple and understandable.

Sometimes the program we are modifying or debugging will be a library, for the convenience of modification, we may not want to put it under the site-packages, but rather to keep it in the original engineering directory, to facilitate the management of the IDE and version control tools. So how do you get the Python runtime to find this library?

In principle, the Python runtime is essentially a traversal of the Sys.path list when looking for a library file, and if we want to register a new class library in the runtime,

  • Or you have to add a new path to the Sys.path list with code;
  • You have to adjust the PYTHONPATH environment variable;
  • You can either copy the library file to a path that is already in the Sys.path settings (such as the Site-packages directory);

These methods are not convenient. The simplest way is to use . PTH file to implement. Python in the process of traversing a known library file directory, if a. pth file is seen, the path recorded in the file is added to the Sys.path setting, and the. pth file says that the specified library can also be found by the Python runtime environment.

In fact, Easy_install relies on the egg package installation is to rely on the Site-packages directory of the. pth file to add a reference to the egg package implementation. Therefore, the contents of the corresponding. pth file can be modified to enable the uninstallation of the egg packet.

(go) Use PTH files to make Python easy to import the modules you write yourself

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.