Skip to content

Commit

Permalink
Detect Conda when looking for a virtual env (python-poetry#1432)
Browse files Browse the repository at this point in the history
Currently Poetry will detect that it is running in a usual virtual env
created with "python -m venv" and will not create an additional env.

This commit extends this logic to Conda, which uses different
environment variables to indicate running in a virtual env.

Conda's "base" env is treated specially to avoid polluting global
namespace.
  • Loading branch information
maksbotan authored and michielboekhoff committed Oct 29, 2019
1 parent 1548d8c commit 863d15b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,13 @@ def get(self, reload=False): # type: (bool) -> Env
python_minor = env["minor"]

# Check if we are inside a virtualenv or not
in_venv = os.environ.get("VIRTUAL_ENV") is not None
# Conda sets CONDA_PREFIX in its envs, see
# https://github.com/conda/conda/issues/2764
env_prefix = os.environ.get("VIRTUAL_ENV", os.environ.get("CONDA_PREFIX"))
conda_env_name = os.environ.get("CONDA_DEFAULT_ENV")
# It's probably not a good idea to pollute Conda's global "base" env, since
# most users have it activated all the time.
in_venv = env_prefix is not None and conda_env_name != "base"

if not in_venv or env is not None:
# Checking if a local virtualenv exists
Expand Down Expand Up @@ -315,8 +321,8 @@ def get(self, reload=False): # type: (bool) -> Env

return VirtualEnv(venv)

if os.environ.get("VIRTUAL_ENV") is not None:
prefix = Path(os.environ["VIRTUAL_ENV"])
if env_prefix is not None:
prefix = Path(env_prefix)
base_prefix = None
else:
prefix = Path(sys.prefix)
Expand Down

0 comments on commit 863d15b

Please sign in to comment.