Skip to content

Commit

Permalink
fix: take virtualenvs.prefer-active-python into account when gettin…
Browse files Browse the repository at this point in the history
…g current environment
  • Loading branch information
finswimmer authored and neersighted committed Nov 22, 2022
1 parent cef15cc commit 3be157c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from subprocess import CalledProcessError
from typing import TYPE_CHECKING
from typing import Any
from typing import cast

import packaging.tags
import tomlkit
Expand Down Expand Up @@ -556,6 +557,26 @@ def _detect_active_python(self) -> str | None:
)
return executable

def _get_python_version(self) -> tuple[int, int, int]:
version_info = tuple(sys.version_info[:3])

if self._poetry.config.get("virtualenvs.prefer-active-python"):
executable = self._detect_active_python()

if executable:
python_patch = decode(
subprocess.check_output(
list_to_shell_command(
[executable, "-c", GET_PYTHON_VERSION_ONELINER]
),
shell=True,
).strip()
)

version_info = tuple(int(v) for v in python_patch.split(".")[:3])

return cast("tuple[int, int, int]", version_info)

def activate(self, python: str) -> Env:
venv_path = self._poetry.config.virtualenvs_path
cwd = self._poetry.file.parent
Expand Down Expand Up @@ -667,7 +688,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

Expand Down

0 comments on commit 3be157c

Please sign in to comment.