Skip to content

gh-149819: Fix .pth files not loaded in Python subprocesses#149888

Open
IntentBug wants to merge 1 commit into
python:mainfrom
IntentBug:gh-149819-fix-pth-subprocess
Open

gh-149819: Fix .pth files not loaded in Python subprocesses#149888
IntentBug wants to merge 1 commit into
python:mainfrom
IntentBug:gh-149819-fix-pth-subprocess

Conversation

@IntentBug
Copy link
Copy Markdown

After PR gh-149583 (Fix double evaluation of .pth and .site files in venvs), .pth files are no longer loaded in subprocesses started with subprocess.run([sys.executable, ...]).

Root cause: main() seeds known_paths from removeduppaths() with all sys.path entries inherited from the parent process. addsitedir() then skips .pth processing for every directory already in known_paths.

Fix (2 changes in Lib/site.py, +10/−6 lines):

  1. main(): call removeduppaths() for dedup but start known_paths as a fresh empty set
  2. addsitedir(): guard sys.path.append with sitedir not in sys.path to avoid duplicates when called with fresh known_paths

Verification:

  • ./python -m test test_site: 90/90 tests pass (0 failures, 6 skipped)
  • Functional test: .pth files correctly processed in subprocess scenario with empty known_paths
  • Functional test (unpatched): .pth files skipped when known_paths is pre-seeded — bug confirmed

Closes gh-149819

@IntentBug IntentBug requested review from FFY00 and warsaw as code owners May 15, 2026 16:25
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 15, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

After PR pythongh-149583 (Fix double evaluation of .pth and .site files in
venvs), .pth files are no longer loaded in subprocesses started with
subprocess.run([sys.executable, ...]).  The root cause: main() seeds
known_paths from removeduppaths() with all sys.path entries inherited
from the parent process.  addsitedir() then skips .pth processing for
every directory already in known_paths.

Fix:
- main(): call removeduppaths() for dedup but start known_paths as a
  fresh empty set, so that addsitedir() processes .pth files in every
  site-packages directory regardless of inherited sys.path.
- addsitedir(): move known_paths.add() before the sys.path.append and
  guard the append with 'sitedir not in sys.path' to avoid creating
  duplicate entries when called with a fresh known_paths.

This preserves the pythongh-75723 dedup guarantee while allowing subprocesses
to load .pth files.
@IntentBug IntentBug force-pushed the gh-149819-fix-pth-subprocess branch from 3b83675 to b7f6ba1 Compare May 15, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New site.py implementations fails to load .pth files in subprocesses

1 participant