From 6b437a6140c06f11101156e2b8ee45a6e66aa161 Mon Sep 17 00:00:00 2001 From: Nico Stapelbroek Date: Wed, 3 Jun 2020 22:28:27 +0200 Subject: [PATCH 1/3] Stop using PYVENV_LAUNCHER env variable when detecting venvs This seems to lead to false positives as every Homebrew installed Python has this environment variable set. The original comment hints to a workaround so I'd figured we should not give it extra responsibilities. Fixes #4316 --- news/4316.bugfix.rst | 1 + pipenv/environments.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 news/4316.bugfix.rst diff --git a/news/4316.bugfix.rst b/news/4316.bugfix.rst new file mode 100644 index 00000000..66d45840 --- /dev/null +++ b/news/4316.bugfix.rst @@ -0,0 +1 @@ +Fix falsely flagging a Homebrew installed Python as a virtual environment diff --git a/pipenv/environments.py b/pipenv/environments.py index 005d0a2d..d98e36ad 100644 --- a/pipenv/environments.py +++ b/pipenv/environments.py @@ -81,7 +81,7 @@ PIPENV_IS_CI = bool("CI" in os.environ or "TF_BUILD" in os.environ) # HACK: Prevent invalid shebangs with Homebrew-installed Python: # https://bugs.python.org/issue22490 -_OSX_VENV = os.environ.pop("__PYVENV_LAUNCHER__", None) +os.environ.pop("__PYVENV_LAUNCHER__", None) # Load patched pip instead of system pip os.environ["PIP_SHIMS_BASE_MODULE"] = fs_str("pipenv.patched.notpip") @@ -326,7 +326,7 @@ PIPENV_TEST_INDEX = os.environ.get("PIPENV_TEST_INDEX") PIPENV_USE_SYSTEM = False PIPENV_VIRTUALENV = None if "PIPENV_ACTIVE" not in os.environ and not PIPENV_IGNORE_VIRTUALENVS: - PIPENV_VIRTUALENV = os.environ.get("VIRTUAL_ENV") or _OSX_VENV + PIPENV_VIRTUALENV = os.environ.get("VIRTUAL_ENV") PIPENV_USE_SYSTEM = bool(PIPENV_VIRTUALENV) # Internal, tells Pipenv to skip case-checking (slow internet connections). @@ -370,7 +370,7 @@ def is_quiet(threshold=-1): def _is_using_venv(): # type: () -> bool """Check for venv-based virtual environment which sets sys.base_prefix""" - return _OSX_VENV is not None or sys.prefix != getattr(sys, "base_prefix", sys.prefix) + return sys.prefix != getattr(sys, "base_prefix", sys.prefix) def _is_using_virtualenv(): From cce026efbf6abf8b69b3c2051adb5afd30c8223d Mon Sep 17 00:00:00 2001 From: frostming Date: Thu, 4 Jun 2020 09:52:39 +0800 Subject: [PATCH 2/3] Correctly populate the system python path --- news/4315.bugfix.rst | 1 + pipenv/core.py | 2 ++ pipenv/environment.py | 5 ++++- 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 news/4315.bugfix.rst diff --git a/news/4315.bugfix.rst b/news/4315.bugfix.rst new file mode 100644 index 00000000..6b8102b5 --- /dev/null +++ b/news/4315.bugfix.rst @@ -0,0 +1 @@ +Fix a bug that incorrect Python path will be used when ``--system`` flag is on. diff --git a/pipenv/core.py b/pipenv/core.py index 02971f22..9b37ff42 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -1946,6 +1946,8 @@ def do_install( # Automatically use an activated virtualenv. if PIPENV_USE_SYSTEM: system = True + if system: + os.environ["PIPENV_USE_SYSTEM"] = "1" # Check if the file is remote or not if remote: click.echo( diff --git a/pipenv/environment.py b/pipenv/environment.py index fbfe9615..db51af74 100644 --- a/pipenv/environment.py +++ b/pipenv/environment.py @@ -267,7 +267,10 @@ class Environment(object): def python(self): # type: () -> str """Path to the environment python""" - py = vistir.compat.Path(self.script_basedir).joinpath("python").absolute().as_posix() + if os.name == "nt" and not self.is_venv: + py = vistir.compat.Path(self.prefix).joinpath("python").absolute().as_posix() + else: + py = vistir.compat.Path(self.script_basedir).joinpath("python").absolute().as_posix() if not py: return vistir.compat.Path(sys.executable).as_posix() return py From 69c5ceabf25f131cbf2a22bce43a3b0a08a516ba Mon Sep 17 00:00:00 2001 From: frostming Date: Thu, 4 Jun 2020 19:21:26 +0800 Subject: [PATCH 3/3] accepts python as argument --- pipenv/environment.py | 9 ++++++++- pipenv/project.py | 6 ++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pipenv/environment.py b/pipenv/environment.py index db51af74..508b615e 100644 --- a/pipenv/environment.py +++ b/pipenv/environment.py @@ -41,6 +41,7 @@ class Environment(object): def __init__( self, prefix=None, # type: Optional[str] + python=None, # type: Optional[str] is_venv=False, # type: bool base_working_set=None, # type: pkg_resources.WorkingSet pipfile=None, # type: Optional[Union[tomlkit.toml_document.TOMLDocument, TPipfile]] @@ -51,6 +52,9 @@ class Environment(object): self._modules = {'pkg_resources': pkg_resources, 'pipenv': pipenv} self.base_working_set = base_working_set if base_working_set else BASE_WORKING_SET prefix = normalize_path(prefix) + self._python = None + if python is not None: + self._python = vistir.compat.Path(python).absolute().as_posix() self.is_venv = is_venv or prefix != normalize_path(sys.prefix) if not sources: sources = [] @@ -267,12 +271,15 @@ class Environment(object): def python(self): # type: () -> str """Path to the environment python""" + if self._python is not None: + return self._python if os.name == "nt" and not self.is_venv: py = vistir.compat.Path(self.prefix).joinpath("python").absolute().as_posix() else: py = vistir.compat.Path(self.script_basedir).joinpath("python").absolute().as_posix() if not py: - return vistir.compat.Path(sys.executable).as_posix() + py = vistir.compat.Path(sys.executable).as_posix() + self._python = py return py @cached_property diff --git a/pipenv/project.py b/pipenv/project.py index 6ae4e6ac..72331c0b 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -352,12 +352,14 @@ class Project(object): is_venv = is_in_virtualenv() if allow_global and not is_venv: prefix = sys.prefix + python = sys.executable else: prefix = self.virtualenv_location + python = None sources = self.sources if self.sources else [DEFAULT_SOURCE] environment = Environment( - prefix=prefix, is_venv=is_venv, sources=sources, pipfile=self.parsed_pipfile, - project=self + prefix=prefix, python=python, is_venv=is_venv, sources=sources, + pipfile=self.parsed_pipfile, project=self ) pipenv_dist = get_pipenv_dist(pkg="pipenv") if pipenv_dist: