Repository: pypa/pip

Issue:
Pip upgrades virtualenv packages from system-site-packages when it does not need to / should not
**Environment**

* pip version: `pip 20.1.1 from /usr/lib/python3.8/site-packages/pip (python 3.8)`
* Python version: `Python 3.8.6`
* OS: Alpine linux edge (container) / `Linux 42293b24d0c1 5.4.0-48-generic #52-Ubuntu` (kernel)

**Description**
We're running CI jobs in containers using alpine linux (not by choice 🤷‍♂️).  We've got pandas installed from the OS package repo, because otherwise it needs to build from source which takes 45+ minutes.  We create our virtualenv with with `--system-site-packages`.  Our package that we are testing and that we install into this venv depends on `pandas` (with no version specifier).

When pandas recently released a new version, that caused pip to try to pull that new version into the venv, despite an acceptable version already being available via `--system-site-packages`.  I think this is a bug: pip should see that the dependency is available in the venv, and not attempt to upgrade it.  The bug persists if `--upgrade-strategy only-if-needed` is passed as a command line (unsurprisingly, since only-if-needed is already the default).  If I use this recipe to create a constraint file listing the system-installed pandas version, then pip still tries to reinstall that same version from source in the venv:

```
pip freeze | grep pandas > constraint.txt
/path/to/venv/bin/python -m pip install -c contstraint.txt -e /path/to/my_package
```

**Expected behavior**
If a package is installed in a venv via system-site-packages, pip should not try to install another version of that package into the venv if not strictly necessary to do so (i.e. if not asked to via `install -U`, `--upgrade-strategy eager`, a dependency on a specific version in a package's dependencies, etc.)

**Steps to reproduce**
```
$ pip install pandas==1.1.2  # you can use any package here, it doesn't have to be pandas
$ python -m venv --system-site-packages venv
$ ./venv/bin/python -m pip freeze | grep pandas  # verify pandas is available in the venv
pandas==1.1.2
$ ./venv/bin/python -m pip install pandas
Collecting pandas
  Downloading pandas-1.1.3.tar.gz (5.2 MB)
^C
```

**Expected behavior**

As above, but the last command should print:

```
Requirement already satisfied: pandas in /usr/lib/pythonX.Y/site-packages
```


Discussion:
IMO this is a reasonable feature request. pip already does something similar to user/non-user system site-packages installations (a `pip install --user` would not uninstall the non-user installation, only shadows), and a similar logic applies for system/virtualenv site-packages in a allowed-system-site virtual environment.

Resolving patch:
diff --git a/src/pip/_internal/resolution/resolvelib/factory.py b/src/pip/_internal/resolution/resolvelib/factory.py
--- a/src/pip/_internal/resolution/resolvelib/factory.py
+++ b/src/pip/_internal/resolution/resolvelib/factory.py
@@ -98,9 +98,15 @@ def __init__(
         self._editable_candidate_cache = {}  # type: Cache[EditableCandidate]
 
         if not ignore_installed:
+            packages = get_installed_distributions(
+                local_only=False,
+                include_editables=True,
+                editables_only=False,
+                user_only=False,
+                paths=None,
+            )
             self._installed_dists = {
-                canonicalize_name(dist.project_name): dist
-                for dist in get_installed_distributions()
+                canonicalize_name(p.key): p for p in packages
             }
         else:
             self._installed_dists = {}


Test patch: