Skip to content

Commit

Permalink
Fix: try python3 before python in install --system (pypa#5296)
Browse files Browse the repository at this point in the history
Fix a regression from commit dbea3f5 where `pip install --system` tries
to install using the `python` executable first which, on some systems
may point to Python 2.  Instead try `python3` first.
  • Loading branch information
enku authored and yeisonvargasf committed Nov 18, 2022
1 parent df9682f commit 65aa893
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/5296.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an issue when using ``pipenv install --system`` on systems that having the ``python`` executable pointing to Python 2 and a Python 3 executable being ``python3``.
2 changes: 1 addition & 1 deletion pipenv/utils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def project_python(project, system=False):
if not system:
python = project._which("python")
else:
interpreters = [system_which(p) for p in ("python", "python3")]
interpreters = [system_which(p) for p in ("python3", "python")]
interpreters = [i for i in interpreters if i] # filter out not found interpreters
python = interpreters[0] if interpreters else None
if not python:
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,17 @@ def test_invalid_prepare_pip_source_args(self):
sources = [{}]
with pytest.raises(PipenvUsageError):
indexes.prepare_pip_source_args(sources, pip_args=None)

@pytest.mark.utils
def test_project_python_tries_python3_before_python_if_system_is_true(self):
def mock_shutil_which(command, path=None):
if command != "python3":
return f"/usr/bin/{command}"
return "/usr/local/bin/python3"

with mock.patch("pipenv.utils.shell.shutil.which", wraps=mock_shutil_which):
# Setting project to None as system=True doesn't use it
project = None
python = shell.project_python(project, system=True)

assert python == "/usr/local/bin/python3"

0 comments on commit 65aa893

Please sign in to comment.