Basj
Basj

Reputation: 46463

How does .pth files distinguish between lines with path information and lines with Python code?

It seems a .pth file can also mix "folders to add to path" information + also, some Python code, such as import foo. Example in pywin32.pth:

# .pth file for the PyWin32 extensions
win32
win32\lib
Pythonwin
# And some hackery to deal with environments where the post_install script
# isn't run.
import pywin32_bootstrap

How does the .pth file parser distinguish lines for folders, and lines with Python code (import...)? Here is seems ambiguous.

Note: Is there an official specification for the Python .pth file format? (I found some information on https://docs.python.org/3/library/site.html but it's incomplete)

Note 2: it seems the .pth files can also include zip files instead of folders, see python38._pth in embedded Python:

python38.zip
.

# Uncomment to run site.main() automatically
#import site

Upvotes: 1

Views: 127

Answers (1)

Michael Butscher
Michael Butscher

Reputation: 10959

From https://github.com/python/cpython/blob/6abddd9f6afdddc09031989e0deb25e301ecf315/Lib/site.py#L29 (reformatted):

A path configuration file is a file whose name has the form <package>.pth; its contents are additional directories (one per line) to be added to sys.path.

Non-existing directories (or non-directories) are never added to sys.path; no directory is added to sys.path more than once.

Blank lines and lines beginning with '#' are skipped. Lines starting with 'import' are executed.

The part that non-directories aren't added is obviously wrong/outdated. The code doesn't require a directory.

Upvotes: 0

Related Questions