Repository navigation
🐛 fix(activation): honor .pth import precedence - #3653
Merged
Merged
Conversation
gaborbernat
marked this pull request as draft
November 20, 2026 06:33
gaborbernat
force-pushed
the
fix/adko-pth-precedence
branch
3 times, most recently
from
November 20, 2026 08:25
31478da to
9b2cdc9
Compare
activate_this.py ran .pth hooks before moving environment paths ahead of host paths. A same-name host module could win during activation. Put standard library paths first, then environment libraries, then host paths while the hooks run. Restore the existing post-activation order. Fixes pypa#3623
The temporary lookup order grouped environment libraries ahead of paths added by earlier .pth files in the final sys.path. Record additions per library to retain the prior order when lib directories differ. Refs pypa#3623
GraalPy keeps implementation modules outside its standard library directory. The initial lookup list missed them and let environment site-packages shadow runtime modules. Executable .pth lines can also insert paths before existing entries. Record additions regardless of insertion position when restoring the post-activation path. Refs pypa#3623
The base-prefix filter could promote an unrelated host path ahead of the environment if that path was under the interpreter prefix. Match the known standard library locations and GraalPy's implementation module directory instead. Use a dict to count path additions so activation imports no extra module before its temporary search order is installed. Refs pypa#3623
gaborbernat
force-pushed
the
fix/adko-pth-precedence
branch
from
November 20, 2026 09:09
9b2cdc9 to
501d391
Compare
gaborbernat
marked this pull request as ready for review
November 20, 2026 10:09
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.
The old
activate_this.pyprocessed environment.pthfiles before moving environment libraries ahead of host paths. A hook could load a same-name host module for users activating inside a running Python process. The script now puts environment libraries ahead of host paths for those imports while keeping standard library paths first. Fixes #3623..pthimports saw host paths firstsite.addsitedir()processes executable.pthlines during the call. The old path rotation happened afterward, so the hook could resolve a module from the host working directory or site-packages before the environment. The new_add_libraries()orders interpreter library paths, environment libraries, then host paths while the hooks run. Interpreter paths include GraalPy's implementation modules. The script records.pthpath insertions at any position and then restores the original host entries behind the added paths.sequenceDiagram participant P as host Python participant A as activate_this.py participant S as site.addsitedir participant I as import machinery P->>+A: runpy.run_path rect rgb(255, 214, 214) Note over A,I: Before: host path precedes environment A->>+S: process environment .pth S->>+I: import hook module I-->>-S: host copy wins S-->>-A: return end rect rgb(216, 243, 220) Note over A,I: Now: interpreter libraries, environment, host A->>+S: process environment .pth S->>+I: import hook module I-->>-S: environment or interpreter copy wins S-->>-A: return end A-->>-P: restore post-activation sys.pathBehavior changes
An environment
.pthhook now finds an environment module before a same-name module on the host path. Standard library and GraalPy implementation modules retain precedence during.pthprocessing. A.pthhook that inserts a new path at the front keeps that path after activation. The change does not reload modules already present insys.modules. Host path entries keep their relative order after activation.Reported by @oss-scanner: ANT-2026-ADKO6NBD.