Questions of Usage of `_pth` file

Hello guys and I encountered some problems when using ._pth files:

My folder lists like:

- Lib
    - site-packages
    - python38.zip
    - speed.zip
- python3.dll
- python38._pth
- python38.dll
- test.py

In which, speed.zip is my self-defined package. My python38._pth file content is

.\Lib\python38.zip
.\Lib\site-packages
.\Lib\speed.zip
.

import site

and the test.py file is purely print sys.path

import sys
print (sys.path)

when I run python test.py, I cannot get the path I added in the _pth file in the result. I have read the documentation but I still cannot understand how and when the file is included.

Is my way of using this file wrong? Or could anybody please explain how and when the file is included?

I have a .pth file, no underscore, in site-packages, where it belongs. The directory it lists are not included in sys.path. Rather, their contents are treated as if in site-packages. I am not sure where the doc for this is.

I believe that putting code is not recommended. There is an issue to disable allowing this.

Documentation for the ._pth file is here - note that it’s specific to Windows.

Note that none of the normal path calculation is done if the embedding application calls Py_SetPath(). This includes reading a “._pth” file.

The more general documentation for 3.11 is in the overview of how sys.path is initialized. In 3.11, “._pth” files are supported on POSIX. The new “Modules/getpath.py” initialization looks for a “._pth” file beside the library (probably not in POSIX), executable (maybe a symlink), and real executable. On Windows, it first splits off the “.exe” or “.dll” extension from the name of the executable or DLL when forming the name of the “._pth” file.