Skip to content

Commit 28ea70b

Browse files
committed
fix: take virtualenvs.prefer-active-python into account when getting current environment
1 parent 55cd2f1 commit 28ea70b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/poetry/utils/env.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from subprocess import CalledProcessError
2121
from typing import TYPE_CHECKING
2222
from typing import Any
23+
from typing import cast
2324

2425
import packaging.tags
2526
import tomlkit
@@ -556,6 +557,26 @@ def _detect_active_python(self) -> str | None:
556557
)
557558
return executable
558559

560+
def _get_python_version(self) -> tuple[int, int, int]:
561+
version_info = tuple(sys.version_info[:3])
562+
563+
if self._poetry.config.get("virtualenvs.prefer-active-python"):
564+
executable = self._detect_active_python()
565+
566+
if executable:
567+
python_patch = decode(
568+
subprocess.check_output(
569+
list_to_shell_command(
570+
[executable, "-c", GET_PYTHON_VERSION_ONELINER]
571+
),
572+
shell=True,
573+
).strip()
574+
)
575+
576+
version_info = tuple(int(v) for v in python_patch.split(".")[:3])
577+
578+
return cast("tuple[int, int, int]", version_info)
579+
559580
def activate(self, python: str) -> Env:
560581
venv_path = self._poetry.config.virtualenvs_path
561582
cwd = self._poetry.file.parent
@@ -667,7 +688,7 @@ def get(self, reload: bool = False) -> Env:
667688
if self._env is not None and not reload:
668689
return self._env
669690

670-
python_minor = ".".join([str(v) for v in sys.version_info[:2]])
691+
python_minor = ".".join([str(v) for v in self._get_python_version()[:2]])
671692

672693
venv_path = self._poetry.config.virtualenvs_path
673694

0 commit comments

Comments
 (0)