Skip to content

Commit 127134a

Browse files
committed
Switch to using lstat() instead of stat() to not match symlink targets.
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.
1 parent f5ff4fa commit 127134a

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

news/13156.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Show the correct path to the interpreter also when it's a symlink in a venv in the pip upgrade prompt.

src/pip/_internal/utils/entrypoints.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def get_best_invocation_for_this_python() -> str:
7777

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

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

0 commit comments

Comments
 (0)