I see two diffrent behaviours about .pth and ._pth files:
the official Python install for Windows uses
.pthfiles inlib\site-packagesas documented in https://docs.python.org/3.12/library/site.html#module-site. It seems that adding multiple.pthfiles works and is used at the Python interpreter startup.the Python Embedded package for Windows seems to use
._pthfiles (and not.pth!) for the same thing:python38._pth, but in the same folder aspython.exe. In this case it seems that adding a second.pthor._pthfile in the same folder isn't used by the interpreter. Why? Is it documented that, for embedded Python, only one ._pth will be used?
More generally, what are the rules about .pth vs ._pth files? In which case should we use one or the other, what is the reason for the underscore _pth?
Note: the rules seem here complicated (.pth vs ._pth ; multiple files allowed vs. only one file used).
NB: Documentation about ._pth files
From what I understand of the documentation provided, I see:
.pthFiles: used in standard Python installations to add extra directories to Python's module search path (sys.path).The
sitemodule processes these files at startup, allowing for the dynamic extension of the search path. Multiple.pthfiles are supported, offering flexibility in configuring the Python environment.._pthfiles: introduced to offer a more controlled environment, particularly for Python's embedded distribution.A
._pthfile specifies a fixed set of directories that Python will include in its module search path, overriding the default behavior, including the processing of.pthfiles. That makes sure the embedded Python interpreter operates in a predictable and isolated manner, using only the specified directories. Only one._pthfile is recognized and processed by the interpreter, making sure a singular configuration for the environment.That means that:
without a
._pthfile, Python populates its search path based on a series of fallbacks including the current directory,PYTHONPATHenvironment variable, application paths specified in the registry, and paths relative to the Python home. That process makes sure Python can locate its libraries and modules in various configurations.for applications that bundle Python (especially embedded distributions), a
._pthfile provides a means to precisely control the search path, ignoring other configurations such asPYTHONPATH, registry entries, and even thesitemodule unless explicitly included. That feature is important for maintaining application integrity and avoiding conflicts with other Python installations.You do get from the documentation clear guidelines for distributing Python within an application, including the use of
._pthfiles, managing environment variables, and making sure of the presence of landmark files or directories. These practices help to create a stable and isolated Python runtime environment, which is particularly important for embedded applications or when Python is bundled with another product.In short: While
.pthfiles offer flexibility in extending Python's module search path in standard environments,._pthfiles provide a mechanism for creating a controlled, isolated environment for embedded Python distributions.The "Using Python on Windows / The embeddable package" documentatin focuses on the embeddable package, suggesting that
._pthfiles are primarily relevant in this context for specifying module search paths in a tightly controlled manner.The use cases:
.pthfiles for extending the module search paths,._pthfiles for isolation.python/cpythonissue 86418 mentions "Overriding the._pthfile should just be a matter of replacing the file", so again, I thought there was only one.The documentation "The initialization of the sys.path module search path" states that the presence of a
._pthfile completely overrides the normal initialization ofsys.path, including ignoring all registry and environment variables. That, again, look that only one._pthfile is recognized and processed, as its existence dictates a unique, controlled environment configuration for the Python interpreter.That distinction clarifies that
.pthand._pthfiles are treated differently, with.pthfiles being processed in the standard manner by thesitemodule, while._pthfiles, when present, enforce an isolated environment that disregards other conventional mechanisms for extendingsys.path.