Skip to content

Pip upgrades virtualenv packages from system-site-packages when it does not need to / should not #8963

@aecay

Description

@aecay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: upgradeThe logic of upgrading packages

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions