GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-149819: fix .pth and .start file processing in subprocess when inheriting PYTHONPATH by warsaw · Pull Request #150177 · python/cpython

Conversation

@warsaw

@warsaw warsaw commented May 21, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Followup to #149888 and #149819 - Fix by @IntentBug in the original PR, with test cases and review by @warsaw

  • Issue: New site.py implementation fails to load .pth files in subprocesses #149819

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tcely reacted with thumbs up emoji tcely reacted with rocket emoji
All reactions
  • 👍 1 reaction
  • 🚀 1 reaction
BugBounty Mind and others added 2 commits May 20, 2026 11:57
BugBounty Mind @warsaw
pythongh-149819: Fix .pth files not loaded in Python subprocesses
a07037d
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.
@warsaw
Fill out the tests for GH#149888
201053d
* Extend _make_start() and _make_pth() to take an optional `basedir` which is used instead of
 `site.tmpdir` if given.
* Add test_pth_processed_when_sitedir_already_on_path() to test the core GH#149819 bug: .pth files
  in subprocesses aren't handled if PYTHONPATH pointing to the .pth directory is inherited.
* Similarly add test_start_processed_when_sitedir_already_on_path() to verify that .start files in
  the same circumstances are also now processed.
@warsaw warsaw self-assigned this May 21, 2026
@warsaw
warsaw requested a review from FFY00 as a code owner May 21, 2026 05:44
@warsaw warsaw added the needs backport to 3.15 bugs and security fixes label May 21, 2026
@bedevere-app bedevere-app Bot mentioned this pull request May 21, 2026
New site.py implementation fails to load .pth files in subprocesses #149819
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label May 21, 2026
@warsaw

warsaw commented May 21, 2026

Copy link
Copy Markdown
Member Author

Also, reproducer by @tcely

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@warsaw

warsaw commented May 21, 2026

Copy link
Copy Markdown
Member Author

Also, original bug report by @scoder

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@warsaw
Merge branch 'main' into pythongh-149819-fix-pth-subprocess
9c53ee4
@tcely

tcely commented May 21, 2026

Copy link
Copy Markdown

This looks good to me. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

scoder
scoder approved these changes May 21, 2026
View reviewed changes
Comment thread Lib/site.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels May 21, 2026
@scoder

scoder commented May 21, 2026

Copy link
Copy Markdown
Contributor

Thanks for working on the site.py issues, BTW. That tends to be hairy business, regularly risking to break the setup of random people in some spot of the globe. I do appreciate it.

tcely and warsaw reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@warsaw @scoder
Update Lib/site.py
78094f8
Co-authored-by: scoder <[email protected]>
@warsaw

warsaw commented May 21, 2026

Copy link
Copy Markdown
Member Author

Thanks for working on the site.py issues, BTW. That tends to be hairy business, regularly risking to break the setup of random people in some spot of the globe. I do appreciate it.

And we had few core team members committed to maintaining site.py, but it seems like a natural place for me to get back involved in. Thank you both for engaging on this issue. I'm close to committing the branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

warsaw added 2 commits May 21, 2026 13:04
@warsaw
Merge branch 'main' into pythongh-149819-fix-pth-subprocess
2575e1d
@warsaw
Oops! Remove redundant code
d289f15
@warsaw
warsaw enabled auto-merge (squash) May 21, 2026 20:20
@warsaw
warsaw merged commit 3c298e2 into python:main May 21, 2026
95 of 97 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented May 21, 2026

Copy link
Copy Markdown

Thanks @warsaw for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label May 21, 2026
@bedevere-app

bedevere-app Bot commented May 21, 2026

Copy link
Copy Markdown

GH-150202 is a backport of this pull request to the 3.15 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label May 21, 2026
@warsaw
warsaw deleted the gh-149819-fix-pth-subprocess branch May 21, 2026 22:29
warsaw added a commit that referenced this pull request May 21, 2026
@miss-islington @warsaw
BugBounty Mind @scoder
[3.15] gh-149819: fix .pth and .start file processing in subprocess w…
233cf75
…hen inheriting PYTHONPATH (GH-150177) (#150202)

gh-149819: fix .pth and .start file processing in subprocess when inheriting PYTHONPATH (GH-150177)

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

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, ...]).  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 gh-75723 dedup guarantee while allowing subprocesses
to load .pth files.

* Fill out the tests for GH#149888

* Extend _make_start() and _make_pth() to take an optional `basedir` which is used instead of
 `site.tmpdir` if given.
* Add test_pth_processed_when_sitedir_already_on_path() to test the core GH#149819 bug: .pth files
  in subprocesses aren't handled if PYTHONPATH pointing to the .pth directory is inherited.
* Similarly add test_start_processed_when_sitedir_already_on_path() to verify that .start files in
  the same circumstances are also now processed.

* Update Lib/site.py



* Oops!  Remove redundant code

---------
(cherry picked from commit 3c298e2)

Co-authored-by: Barry Warsaw <[email protected]>
Co-authored-by: BugBounty Mind <[email protected]>
Co-authored-by: scoder <[email protected]>
@warsaw warsaw mentioned this pull request May 21, 2026
gh-149819: Fix .pth files not loaded in Python subprocesses #149888
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@scoder scoder scoder approved these changes
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner
+1 more reviewer
@tcely tcely tcely left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@warsaw warsaw

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@warsaw @tcely @scoder
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.