diff --git a/news/5296.bugfix.rst b/news/5296.bugfix.rst new file mode 100644 index 0000000000..7af9507b41 --- /dev/null +++ b/news/5296.bugfix.rst @@ -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``. diff --git a/pipenv/utils/shell.py b/pipenv/utils/shell.py index 362290fae9..f6e39bd086 100644 --- a/pipenv/utils/shell.py +++ b/pipenv/utils/shell.py @@ -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: diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 788b23eeaa..b7477b6b82 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -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"