From efab161029ab8bd326cb43ccefdc27d43a913eb0 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Sun, 6 Nov 2022 18:52:50 +0100 Subject: [PATCH] fix: take `virtualenvs.prefer-active-python` into account when getting current environment --- src/poetry/utils/env.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/poetry/utils/env.py b/src/poetry/utils/env.py index b95a83aeb10..c79fa4a8378 100644 --- a/src/poetry/utils/env.py +++ b/src/poetry/utils/env.py @@ -556,6 +556,22 @@ def _detect_active_python(self) -> str | None: ) return executable + def _get_python_version(self) -> list[int]: + if self._poetry.config.get("virtualenvs.prefer-active-python"): + executable = self._detect_active_python() + python_patch = decode( + subprocess.check_output( + list_to_shell_command( + [executable, "-c", GET_PYTHON_VERSION_ONELINER] + ), + shell=True, + ).strip() + ) + + return [int(v) for v in python_patch.split(".")[:3]] + + return sys.version_info[:3] + def activate(self, python: str) -> Env: venv_path = self._poetry.config.virtualenvs_path cwd = self._poetry.file.parent @@ -667,7 +683,7 @@ def get(self, reload: bool = False) -> Env: if self._env is not None and not reload: return self._env - python_minor = ".".join([str(v) for v in sys.version_info[:2]]) + python_minor = ".".join([str(v) for v in self._get_python_version()[:2]]) venv_path = self._poetry.config.virtualenvs_path