From 2c3e51d22abddd3abc45d21eae9df9bb3ed8d4ab Mon Sep 17 00:00:00 2001 From: Albert Hopkins Date: Fri, 26 Aug 2022 15:40:07 +0000 Subject: [PATCH] Fix: try python3 before python in `install --system` (#5296) 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. --- news/5296.bugfix.rst | 1 + pipenv/utils/shell.py | 2 +- tests/unit/test_utils.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 news/5296.bugfix.rst 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"