diff --git a/src/poetry/utils/env.py b/src/poetry/utils/env.py index f3cfac527fe..7102a862fd1 100644 --- a/src/poetry/utils/env.py +++ b/src/poetry/utils/env.py @@ -465,6 +465,21 @@ class EnvManager: def __init__(self, poetry: "Poetry") -> None: self._poetry = poetry + def _full_python_path(self, python: str) -> str: + try: + executable = decode( + subprocess.check_output( + list_to_shell_command( + [python, "-c", '"import sys; print(sys.executable)"'] + ), + shell=True, + ).strip() + ) + except CalledProcessError as e: + raise EnvCommandError(e) + + return executable + def _detect_active_python(self, io: "IO") -> str: executable = None @@ -474,14 +489,7 @@ def _detect_active_python(self, io: "IO") -> str: " config.", verbosity=Verbosity.VERBOSE, ) - executable = decode( - subprocess.check_output( - list_to_shell_command( - ["python", "-c", '"import sys; print(sys.executable)"'] - ), - shell=True, - ).strip() - ) + executable = self._full_python_path("python") io.write_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE) except CalledProcessError: io.write_line( @@ -511,6 +519,8 @@ def activate(self, python: str, io: "IO") -> "Env": # Executable in PATH or full executable path pass + python = self._full_python_path(python) + try: python_version = decode( subprocess.check_output( diff --git a/tests/console/commands/env/helpers.py b/tests/console/commands/env/helpers.py index bd4f486d867..23d1e1e8d1c 100644 --- a/tests/console/commands/env/helpers.py +++ b/tests/console/commands/env/helpers.py @@ -25,6 +25,8 @@ def check_output(cmd: str, *_: Any, **__: Any) -> str: return version.text elif "sys.version_info[:2]" in cmd: return f"{version.major}.{version.minor}" + elif '-c "import sys; print(sys.executable)"' in cmd: + return f"/usr/bin/{cmd.split()[0]}" else: return str(Path("/prefix")) diff --git a/tests/console/commands/env/test_use.py b/tests/console/commands/env/test_use.py index 6e7be093387..3dc32a4e76b 100644 --- a/tests/console/commands/env/test_use.py +++ b/tests/console/commands/env/test_use.py @@ -69,7 +69,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file( venv_py37 = venv_cache / f"{venv_name}-py3.7" mock_build_env.assert_called_with( venv_py37, - executable="python3.7", + executable="/usr/bin/python3.7", flags={"always-copy": False, "system-site-packages": False}, with_pip=True, with_setuptools=True, diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index ee8c4ee9ee6..3b8e33920fd 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -154,12 +154,14 @@ def build_venv(path: Union[Path, str], **__: Any) -> None: def check_output_wrapper( version: Version = VERSION_3_7_1, -) -> Callable[[List[str], Any, Any], str]: - def check_output(cmd: List[str], *args: Any, **kwargs: Any) -> str: +) -> Callable[[str, Any, Any], str]: + def check_output(cmd: str, *args: Any, **kwargs: Any) -> str: if "sys.version_info[:3]" in cmd: return version.text elif "sys.version_info[:2]" in cmd: return f"{version.major}.{version.minor}" + elif '-c "import sys; print(sys.executable)"' in cmd: + return f"/usr/bin/{cmd.split()[0]}" else: return str(Path("/prefix")) @@ -193,7 +195,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file( m.assert_called_with( Path(tmp_dir) / f"{venv_name}-py3.7", - executable="python3.7", + executable="/usr/bin/python3.7", flags={"always-copy": False, "system-site-packages": False}, with_pip=True, with_setuptools=True, @@ -328,7 +330,7 @@ def test_activate_activates_different_virtualenv_with_envs_file( m.assert_called_with( Path(tmp_dir) / f"{venv_name}-py3.6", - executable="python3.6", + executable="/usr/bin/python3.6", flags={"always-copy": False, "system-site-packages": False}, with_pip=True, with_setuptools=True, @@ -389,7 +391,7 @@ def test_activate_activates_recreates_for_different_patch( build_venv_m.assert_called_with( Path(tmp_dir) / f"{venv_name}-py3.7", - executable="python3.7", + executable="/usr/bin/python3.7", flags={"always-copy": False, "system-site-packages": False}, with_pip=True, with_setuptools=True, @@ -1022,7 +1024,7 @@ def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir( m.assert_called_with( poetry.file.parent / ".venv", - executable="python3.7", + executable="/usr/bin/python3.7", flags={"always-copy": False, "system-site-packages": False}, with_pip=True, with_setuptools=True,