Skip to content

Commit 24c22a3

Browse files
committed
Check the argument to --python is executable
1 parent 78e7ea8 commit 24c22a3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/pip/_internal/cli/main_parser.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,20 @@ def identify_python_interpreter(python: str) -> Optional[str]:
7171
if py:
7272
return py
7373

74-
# TODO: On Windows, `--python .venv/Scripts/python` won't pass the
75-
# exists() check (no .exe extension supplied). But it's pretty
76-
# obvious what the user intends. Should we allow this?
74+
# If the named file exists, and is executable, use it.
75+
# If it's a directory, assume it's a virtual environment and
76+
# look for the environment's Python executable.
7777
if os.path.exists(python):
78-
if not os.path.isdir(python):
78+
# Do the directory check first because directories can be executable
79+
if os.path.isdir(python):
80+
# bin/python for Unix, Scripts/python.exe for Windows
81+
# Try both in case of odd cases like cygwin.
82+
for exe in ("bin/python", "Scripts/python.exe"):
83+
py = os.path.join(python, exe)
84+
if os.path.exists(py):
85+
return py
86+
elif os.access(python, os.X_OK):
7987
return python
80-
# Might be a virtual environment
81-
for exe in ("bin/python", "Scripts/python.exe"):
82-
py = os.path.join(python, exe)
83-
if os.path.exists(py):
84-
return py
8588

8689
# Could not find the interpreter specified
8790
return None

0 commit comments

Comments
 (0)