@@ -472,7 +472,7 @@ def get(self, reload=False): # type: (bool) -> Env
472
472
create_venv = self ._poetry .config .get ("virtualenvs.create" , True )
473
473
474
474
if not create_venv :
475
- return SystemEnv ( Path ( sys . prefix ) )
475
+ return self . get_system_env ( )
476
476
477
477
venv_path = self ._poetry .config .get ("virtualenvs.path" )
478
478
if venv_path is None :
@@ -485,7 +485,7 @@ def get(self, reload=False): # type: (bool) -> Env
485
485
venv = venv_path / name
486
486
487
487
if not venv .exists ():
488
- return SystemEnv ( Path ( sys . prefix ) )
488
+ return self . get_system_env ( )
489
489
490
490
return VirtualEnv (venv )
491
491
@@ -790,7 +790,7 @@ def create_venv(
790
790
p_venv = os .path .normcase (str (venv ))
791
791
if any (p .startswith (p_venv ) for p in paths ):
792
792
# 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 () ))
794
794
795
795
return VirtualEnv (venv )
796
796
@@ -833,7 +833,33 @@ def remove_venv(cls, path): # type: (Union[Path,str]) -> None
833
833
elif file_path .is_dir ():
834
834
shutil .rmtree (str (file_path ))
835
835
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
837
863
if hasattr (sys , "real_prefix" ):
838
864
return sys .real_prefix
839
865
@@ -993,7 +1019,7 @@ def supported_tags(self): # type: () -> List[Tag]
993
1019
return self ._supported_tags
994
1020
995
1021
@classmethod
996
- def get_base_prefix (cls ): # type: () -> Path
1022
+ def get_base_prefix (cls ): # type: () -> str
997
1023
if hasattr (sys , "real_prefix" ):
998
1024
return sys .real_prefix
999
1025
0 commit comments