Skip to content

Commit 9246cf3

Browse files
committed
Ensure we don't pick up Poetry's virtualenv as the system env
1 parent 67c1b34 commit 9246cf3

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

poetry/locations.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import os
2+
13
from .utils._compat import Path
24
from .utils.appdirs import user_cache_dir
35
from .utils.appdirs import user_config_dir
6+
from .utils.appdirs import user_data_dir
47

58

69
CACHE_DIR = user_cache_dir("pypoetry")
710
CONFIG_DIR = user_config_dir("pypoetry")
811

912
REPOSITORY_CACHE_DIR = Path(CACHE_DIR) / "cache" / "repositories"
13+
14+
15+
def data_dir(): # type: () -> Path
16+
if os.getenv("POETRY_HOME"):
17+
return Path(os.getenv("POETRY_HOME")).expanduser()
18+
19+
return Path(user_data_dir("pypoetry", roaming=True))

poetry/utils/env.py

+31-5
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ def get(self, reload=False): # type: (bool) -> Env
472472
create_venv = self._poetry.config.get("virtualenvs.create", True)
473473

474474
if not create_venv:
475-
return SystemEnv(Path(sys.prefix))
475+
return self.get_system_env()
476476

477477
venv_path = self._poetry.config.get("virtualenvs.path")
478478
if venv_path is None:
@@ -485,7 +485,7 @@ def get(self, reload=False): # type: (bool) -> Env
485485
venv = venv_path / name
486486

487487
if not venv.exists():
488-
return SystemEnv(Path(sys.prefix))
488+
return self.get_system_env()
489489

490490
return VirtualEnv(venv)
491491

@@ -790,7 +790,7 @@ def create_venv(
790790
p_venv = os.path.normcase(str(venv))
791791
if any(p.startswith(p_venv) for p in paths):
792792
# Running properly in the virtualenv, don't need to do anything
793-
return SystemEnv(Path(sys.prefix), self.get_base_prefix())
793+
return SystemEnv(Path(sys.prefix), Path(self.get_base_prefix()))
794794

795795
return VirtualEnv(venv)
796796

@@ -833,7 +833,33 @@ def remove_venv(cls, path): # type: (Union[Path,str]) -> None
833833
elif file_path.is_dir():
834834
shutil.rmtree(str(file_path))
835835

836-
def get_base_prefix(self): # type: () -> Path
836+
@classmethod
837+
def get_system_env(cls, naive=False): # type: (bool) -> "SystemEnv"
838+
"""
839+
Retrieve the current Python environment.
840+
This can be the base Python environment or an activated virtual environment.
841+
This method also works around the issue that the virtual environment
842+
used by Poetry internally (when installed via the custom installer)
843+
is incorrectly detected as the system environment. Note that this workaround
844+
happens only when `naive` is False since there are times where we actually
845+
want to retrieve Poetry's custom virtual environment
846+
(e.g. plugin installation or self update).
847+
"""
848+
prefix, base_prefix = Path(sys.prefix), Path(cls.get_base_prefix())
849+
if naive is False:
850+
from poetry.locations import data_dir
851+
852+
try:
853+
prefix.relative_to(data_dir())
854+
except ValueError:
855+
pass
856+
else:
857+
prefix = base_prefix
858+
859+
return SystemEnv(prefix)
860+
861+
@classmethod
862+
def get_base_prefix(cls): # type: () -> str
837863
if hasattr(sys, "real_prefix"):
838864
return sys.real_prefix
839865

@@ -993,7 +1019,7 @@ def supported_tags(self): # type: () -> List[Tag]
9931019
return self._supported_tags
9941020

9951021
@classmethod
996-
def get_base_prefix(cls): # type: () -> Path
1022+
def get_base_prefix(cls): # type: () -> str
9971023
if hasattr(sys, "real_prefix"):
9981024
return sys.real_prefix
9991025

0 commit comments

Comments
 (0)