Skip to content

Commit

Permalink
Switch to using lstat() instead of stat() to not match symlink targets.
Browse files Browse the repository at this point in the history
The convenience function samefile() calls stat() and samestat(), but
this leads to treating a symlink and its target as the same which is
unlikely to be intentional here as that doesn't work well with venv.
  • Loading branch information
roubert committed Jan 30, 2025
1 parent f5ff4fa commit 625403a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/13156.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show the correct path to the interpreter also when it's a symlink in a venv.
5 changes: 4 additions & 1 deletion src/pip/_internal/utils/entrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ def get_best_invocation_for_this_python() -> str:

# Try to use the basename, if it's the first executable.
found_executable = shutil.which(exe_name)
if found_executable and os.path.samefile(found_executable, exe):
# Virtual environments often symlink to their parent Python binaries, but we don't
# want to treat the Python binaries as equivalent when the environment's Python is
# not on PATH (not activated). Thus, we don't follow symlinks.
if found_executable and os.path.samestat(os.lstat(found_executable), os.lstat(exe)):
return exe_name

# Use the full executable name, because we couldn't find something simpler.
Expand Down

0 comments on commit 625403a

Please sign in to comment.