diff --git a/news/4412.bugfix.rst b/news/4412.bugfix.rst new file mode 100644 index 00000000..6630ebcd --- /dev/null +++ b/news/4412.bugfix.rst @@ -0,0 +1 @@ +Correctly detect whether Pipenv in run under an activated virtualenv. diff --git a/pipenv/environments.py b/pipenv/environments.py index d98e36ad..945e1930 100644 --- a/pipenv/environments.py +++ b/pipenv/environments.py @@ -367,16 +367,16 @@ def is_quiet(threshold=-1): return PIPENV_VERBOSITY <= threshold -def _is_using_venv(): +def is_using_venv(): # type: () -> bool """Check for venv-based virtual environment which sets sys.base_prefix""" - return sys.prefix != getattr(sys, "base_prefix", sys.prefix) - - -def _is_using_virtualenv(): - # type: () -> bool - """Check for virtualenv-based environment which sets sys.real_prefix""" - return getattr(sys, "real_prefix", None) is not None + if getattr(sys, 'real_prefix', None) is not None: + # virtualenv venvs + result = True + else: + # PEP 405 venvs + result = sys.prefix != getattr(sys, 'base_prefix', sys.prefix) + return result def is_in_virtualenv(): @@ -388,16 +388,9 @@ def is_in_virtualenv(): """ pipenv_active = os.environ.get("PIPENV_ACTIVE", False) - virtual_env = None - use_system = False + virtual_env = bool(os.environ.get("VIRTUAL_ENV")) ignore_virtualenvs = bool(os.environ.get("PIPENV_IGNORE_VIRTUALENVS", False)) - - if not pipenv_active and not ignore_virtualenvs: - virtual_env = any([ - _is_using_virtualenv(), _is_using_venv(), os.environ.get("VIRTUAL_ENV") - ]) - use_system = bool(virtual_env) - return (use_system or virtual_env) and not (pipenv_active or ignore_virtualenvs) + return virtual_env and not (pipenv_active or ignore_virtualenvs) PIPENV_SPINNER_FAIL_TEXT = fix_utf8(u"✘ {0}") if not PIPENV_HIDE_EMOJIS else ("{0}") diff --git a/pipenv/project.py b/pipenv/project.py index 72331c0b..f3016d40 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -25,7 +25,7 @@ from .environment import Environment from .environments import ( PIPENV_DEFAULT_PYTHON_VERSION, PIPENV_IGNORE_VIRTUALENVS, PIPENV_MAX_DEPTH, PIPENV_PIPFILE, PIPENV_PYTHON, PIPENV_TEST_INDEX, PIPENV_VENV_IN_PROJECT, - PIPENV_USE_SYSTEM, is_in_virtualenv, is_type_checking + PIPENV_USE_SYSTEM, is_in_virtualenv, is_type_checking, is_using_venv ) from .vendor.requirementslib.models.utils import get_default_pyproject_backend from .utils import ( @@ -349,7 +349,7 @@ class Project(object): def get_environment(self, allow_global=False): # type: (bool) -> Environment - is_venv = is_in_virtualenv() + is_venv = is_in_virtualenv() or is_using_venv() if allow_global and not is_venv: prefix = sys.prefix python = sys.executable