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.
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 thesite-packages/virtualenv_support
directory (it getssetuptools
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: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 atar.gz
, but other than that it acts the same way (thanks @wim for the update).You cannot downgrade pip using pip, the solution is to install a specific version in your virtual environment:
This will allow you to keep using
--process-dependency-links
that was removed in pip 19.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:
A new addition to the stdlib
venv
module isEnvBuilder.upgrade_dependencies
. However, it didn't make it into thevenv
command-line interface in time for the 3.8.0 release.Unfortunately, it won't really help users to install a specific pip version, only the latest.
All is not lost! The
venv
CLI does provide 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). This is all a bit convoluted, so best to put it into a shell function - this goes into your shell profile or rc file:As written, this function just pulls latest
pip
,setuptools
, andwheel
from index. To force specific versions you can just change this line of the shell script:Into this, for example:
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 thepathlib
usage intoos.path
+glob
.For me, I just upgraded pip/virtualenv/virtualenvwrapper on my machine (not inside the virtualenv). Subsequently created virtualenvs had the updated version.