gh-149819: fix .pth and .start file processing in subprocess when inheriting PYTHONPATH#150177
Open
warsaw wants to merge 3 commits into
Open
gh-149819: fix .pth and .start file processing in subprocess when inheriting PYTHONPATH#150177warsaw wants to merge 3 commits into
warsaw wants to merge 3 commits into
Conversation
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.
* 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.
Member
Author
|
Also, reproducer by @tcely |
Member
Author
|
Also, original bug report by @scoder |
|
This looks good to me. Thanks! |
scoder
approved these changes
May 21, 2026
| # Fix __file__ of already imported modules too. | ||
| abs_paths() | ||
|
|
||
| known_paths = venv(known_paths) |
Contributor
There was a problem hiding this comment.
I can't see a reason to set known_paths above and reassign it here.
Suggested change
| known_paths = venv(known_paths=set()) |
There was a problem hiding this comment.
This change makes sense. My first suspicion was that abs_paths() might be using that empty set in some way.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Followup to #149888 and #149819 - Fix by @IntentBug in the original PR, with test cases and review by @warsaw