Control the pip version in virtualenv

Question:

How do I control the version of pip which is used in a freshly created venv?

By default, it uses a vendored pip distribution which may be out of date or unsuitable for whatever other reason. I want to be able to create a venv with a user-specified version of pip installed initially, as opposed to creating one and then upgrading the pip installation from within the env.

Asked By: wim

||

Answers:

It’s easy enough to replace the pip that gets installed in your virtual environment. Within your virtual environment active, simply execute the following command:

pip install pip==1.4.1
Answered By: Wolf

From reading the source of virtualenv, it looks like pip is installed from a source tarfile included with virtualenv. In virtualenv 1.10.1, it is pip-1.4.1.tar.gz in the site-packages/virtualenv_support directory (it gets setuptools from the same place). You could feasibly replace that archive to control the version; virtualenv.py, at least the version I have, doesn’t care which version of pip is there:

    if not no_pip:
        install_sdist('Pip', 'pip-*.tar.gz', py_executable, search_dirs)

You could also pass the --no-pip option and then install the version you want from source.

In virtualenv 1.11, it looks for a wheel file (e.g. pip-*.whl) instead of a tar.gz, but other than that it acts the same way (thanks @wim for the update).

Answered By: Sam Hartsfield

For me, I just upgraded pip/virtualenv/virtualenvwrapper on my machine (not inside the virtualenv). Subsequently created virtualenvs had the updated version.

deactivate
pip install --upgrade pip virtualenv virtualenvwrapper
mkvirtualenv ...
Answered By: KFunk

You cannot downgrade pip using pip, the solution is to install a specific version in your virtual environment:

virtualenv env -p python3.6 --no-pip
source env/bin/activate
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py pip==18.1

This will allow you to keep using --process-dependency-links that was removed in pip 19.

Answered By: FelixEnescu

Since Python 3.9 the stdlib venv module has EnvBuilder.upgrade_dependencies. Unfortunately, it has two shortcomings:

  • Won’t really help users to install a specific pip version, only the latest.
  • It still installs the vendored pip and setuptools versions first, and then uninstall them if they’re outdated, which they almost always will be in practice.

It would be ideal to install the latest versions directly! The venv CLI provides a --without-pip argument that is useful here. You can use this to opt-out of the vendored pip, and then actually use the vendored pip wheel to install your desired pip version instead (along with any other packages you might want in a freshly created virtual environment).

It’s best to put it into a function – this goes into your shell profile or rc file:

function ve() {
    local py="python3"
    if [ ! -d ./.venv ]; then
        echo "creating venv..."
        if ! $py -m venv .venv --prompt=$(basename $PWD) --without-pip; then
            echo "ERROR: Problem creating venv" >&2
            return 1
        else
            local whl=$($py -c "import pathlib, ensurepip; [whl] = pathlib.Path(ensurepip.__path__[0]).glob('_bundled/pip*.whl'); print(whl)")
            echo "boostrapping pip using $whl"
            .venv/bin/python $whl/pip install --upgrade pip setuptools wheel
            source .venv/bin/activate
        fi
    else
        source .venv/bin/activate
    fi
}

As written, this function just pulls latest pip, setuptools, and wheel from index. To force specific versions you can just change this line of the shell script:

.venv/bin/python $whl/pip install --upgrade pip setuptools wheel

Into this, for example:

.venv/bin/python $whl/pip install pip==19.3.1

For Python 2.7 users, you may do a similar trick because virtualenv provides similar command-line options in --no-pip, --no-setuptools, and --no-wheel, and there is still a vendored pip wheel available to bootstrap since Python 2.7.9. Pathlib will not be available, so you’ll need to change the pathlib usage into os.path + glob.

Answered By: wim

TLDR

python -m pip install --upgrade pip==<target version number>

Example

Downgrading from pip 20.3 to pip 19.3 from within a virtual environment.

(env) $ pip --version
pip 20.3.1

(env) $ python -m pip install --upgrade pip==19.3          # downgrading
Collecting pip==19.3
  Using cached pip-19.3-py2.py3-none-any.whl (1.4 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.3.1
    Uninstalling pip-20.3.1:
      Successfully uninstalled pip-20.3.1
Successfully installed pip-19.3

(env) $pip --version                                                                     trex@Tobiahs-MacBook-Pro
pip 19.3 
Answered By: Tobiah Rex

While creating virtual environment using venv module, use optional argument –upgrade-deps.

That will upgrade pip + setuptools to the latest on PyPI.

Example : python3 -m venv –upgrade-deps .venv

Reference link :
venv module documentation

It indicates "Changed in version 3.9: Add –upgrade-deps option to upgrade pip + setuptools to the latest on PyPI"

Note : I tried this using Python 3.10.4

Answered By: Ajay Chauhan

Solved the same issue today on my windows machine with python 3.10.2 installed.

  1. download required pip wheel from history to pathtopythonlibensurepipbundled
  2. in pathtopythonlibensurepip__init__.py change _PIP_VERSION = your version
  3. create environment as usual python -m venv pathtoenv
Answered By: LuettgeM

I had issues with pip 22.3.1, so I wanted to downgrade it to 22.3, while pip 22.3.1 produces errors and not letting me downgrade as the other solutions suggest.

I solved the issue by creating a new venv with the specific pip version, as follows:

virtualenv env -p python3.10 --pip 22.3

Answered By: AKMalkadi