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