From 5c95ec9b9b572f158b2012f6c2b6015683957625 Mon Sep 17 00:00:00 2001 From: Dan Ryan Date: Tue, 19 May 2020 17:01:11 -0400 Subject: [PATCH] Load the pip version from the correct prefix - Respect `--system` when it is supplied during install - Fixes #4220 Signed-off-by: Dan Ryan --- news/4220.bugfix.rst | 1 + pipenv/core.py | 9 ++++++--- pipenv/project.py | 35 ++++++++++++++++++++++------------- 3 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 news/4220.bugfix.rst diff --git a/news/4220.bugfix.rst b/news/4220.bugfix.rst new file mode 100644 index 0000000000..4a31e623fe --- /dev/null +++ b/news/4220.bugfix.rst @@ -0,0 +1 @@ +Fixed a bug which caused pipenv to search non-existent virtual environments for ``pip`` when installing using ``--system``. diff --git a/pipenv/core.py b/pipenv/core.py index b28af3491c..b83ac58ce0 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -1335,8 +1335,10 @@ def get_pip_args( no_deps=False, # type: bool, selective_upgrade=False, # type: bool src_dir=None, # type: Optional[str] + allow_global=False, # type: bool ): # type: (...) -> List[str] + from .environment import Environment from .vendor.packaging.version import parse as parse_version arg_map = { "pre": ["--pre"], @@ -1352,9 +1354,10 @@ def get_pip_args( ], "src_dir": src_dir, } - if project.environment.pip_version >= parse_version("19.0"): + environment = project.get_environment(allow_global=allow_global) + if environment.pip_version >= parse_version("19.0"): arg_map["no_use_pep517"].append("--no-use-pep517") - if project.environment.pip_version < parse_version("19.1"): + if environment.pip_version < parse_version("19.1"): arg_map["no_use_pep517"].append("--no-build-isolation") arg_set = [] for key in arg_map.keys(): @@ -1495,7 +1498,7 @@ def pip_install( pip_args = get_pip_args( pre=pre, verbose=environments.is_verbose(), upgrade=True, selective_upgrade=selective_upgrade, no_use_pep517=not use_pep517, - no_deps=no_deps, require_hashes=not ignore_hashes + no_deps=no_deps, require_hashes=not ignore_hashes, allow_global=allow_global ) pip_command.extend(pip_args) if r: diff --git a/pipenv/project.py b/pipenv/project.py index ee2219ef05..40b6b26364 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -25,7 +25,7 @@ from .environments import ( PIPENV_DEFAULT_PYTHON_VERSION, PIPENV_IGNORE_VIRTUALENVS, PIPENV_MAX_DEPTH, PIPENV_PIPFILE, PIPENV_PYTHON, PIPENV_TEST_INDEX, PIPENV_VENV_IN_PROJECT, - is_in_virtualenv, is_type_checking + PIPENV_USE_SYSTEM, is_in_virtualenv, is_type_checking ) from .vendor.requirementslib.models.utils import get_default_pyproject_backend from .utils import ( @@ -328,21 +328,30 @@ def pipfile_package_names(self): "combined": dev_keys | default_keys } + def get_environment(self, allow_global=False): + # type: (bool) -> Environment + if allow_global: + prefix = sys.prefix + else: + prefix = self.virtualenv_location + is_venv = is_in_virtualenv() + 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 + ) + pipenv_dist = get_pipenv_dist(pkg="pipenv") + if pipenv_dist: + environment.extend_dists(pipenv_dist) + else: + environment.add_dist("pipenv") + return environment + @property def environment(self): if not self._environment: - prefix = self.virtualenv_location - is_venv = is_in_virtualenv() - sources = self.sources if self.sources else [DEFAULT_SOURCE] - self._environment = Environment( - prefix=prefix, is_venv=is_venv, sources=sources, pipfile=self.parsed_pipfile, - project=self - ) - pipenv_dist = get_pipenv_dist(pkg="pipenv") - if pipenv_dist: - self._environment.extend_dists(pipenv_dist) - else: - self._environment.add_dist("pipenv") + allow_global = os.environ.get("PIPENV_USE_SYSTEM", PIPENV_USE_SYSTEM) + self._environment = self.get_environment(allow_global=allow_global) return self._environment def get_outdated_packages(self):