30

I'm setting up virtual env. I was getting warnings about an outdated pip (19.2) so I updated pip on my (macos) system globally, sudo -H python3 -m pip install --upgrade pip. It seems to have worked, but when I make a new venv, I'm still getting the old pip version.

% pip --version pip 20.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8) % python3 -m pip --version pip 20.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8) % rm -rf .venv # make sure % python3 -m venv .venv % . .venv/bin/activate (.venv) % python3 -m pip --version pip 19.2.3 from /Users/marvin/.venv/lib/python3.8/site-packages/pip (python 3.8) (.venv) % pip --version pip 19.2.3 from /Users/marvin/.venv/lib/python3.8/site-packages/pip (python 3.8) 

Where is the older version coming from?

0

1 Answer 1

30

Pip is installed anew in any freshly created venv. The venv's default pip version is associated with the Python version, and is completely independent from whatever pip version you may have installed on the system. The older version comes from a wheel file bundled with the stdlib ensurepip module. This allows users to create a venv even with no internet connection available, as the venv docs mention:

Unless the --without-pip option is given, ensurepip will be invoked to bootstrap pip into the virtual environment

You can check the bundled pip version with ensurepip.version:

>>> import ensurepip >>> ensurepip.version() '19.2.3' 

Python 3.8.2 is vendoring pip 19.2.3 and setuptools 41.2.0, matching what you've seen.

To create venvs directly with the latest pip version, rather than creating them with an older pip and then upgrading the pip version, refer to this answer:

How to get “python -m venv” to directly install latest pip version

Sign up to request clarification or add additional context in comments.

4 Comments

Reading sentences above - and not exploring the guts of impl - some things don't make sense (until, maybe, I THINK about it). "make a venv without an internet connection." I have 20.1 installed, no internet needed. But the missing thing I realize is venv isn't just copying files, it's INSTALLING a PACKAGE. It can't just copy some 20.1 files into the venv. So your point about the bundled wheel was the missing piece (for me). I might suggest rewording your answer to point out this distinction. Thanks
@Marvin That “missing thing” is literally the first line in the answer: pip is installed anew in created venv. If you think it could be worded any clearer feel free to edit...
I understood "pip is installed anew". I upgraded my global python pip to 20.1, since obviously the python I'm using is where pythonImUsing -m venv is getting things to install. So the thing that was unclear is not that pip is installed anew it's why. venv didn't use the one in my global site-packages (in pythonImUsing). But, clarity is opinion, your answer, and my tl;dr comments, AND you very helpful referenced answer should probably be sufficient. ;-)
After reading the answers linked here, it seems to me that the most straightforward and cleanest way to accomplish this is actually just to upgrade pip manually every time. Amounts to adding one additional line to your workflow, after you activate the new venv but before installing anything else: python -m pip install -U pip.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.