7

I'm trying to prevent this warning every time I create a fresh .venv:

> /Users/pi/.pyenv/versions/3.10.0/bin/python -m venv .venv > . .venv/bin/activate > pip install ipykernel # or anything WARNING: You are using pip version 21.2.3; however, version 22.2.2 is available. You should consider upgrading via the '/Users/pi/code/foo/.venv/bin/python -m pip install --upgrade pip' command. 

Somehow pyenv has populated my fresh .venv with an out-of-date pip.

If I execute the suggested command it will upgrade my .venv's pip. But I don't want to be doing that every time I create a .venv.

I figured this might fix it, but it doesn't:

> /Users/pi/.pyenv/versions/3.10.0/bin/python -m pip install --upgrade pip Requirement already satisfied: pip in /Users/pi/.pyenv/versions/3.10.0/lib/python3.10/site-packages (22.2.1) Collecting pip Using cached pip-22.2.2-py3-none-any.whl (2.0 MB) Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 22.2.1 Uninstalling pip-22.2.1: Successfully uninstalled pip-22.2.1 Successfully installed pip-22.2.2 

What is actually happening when I execute the above command? I was expecting it to update the pip for the python version created/maintained by pyenv. Which it seems to be doing:

🧢 pi@pPro18-4 ~/.pyenv/versions/3.10.0 > find . -name 'pip*' ./bin/pip3 ./bin/pip ./bin/pip3.10 ./lib/python3.10/site-packages/pip ./lib/python3.10/site-packages/pip-22.2.2.dist-info 🧢 pi@pPro18-4 ~/.pyenv/versions/3.10.0 > ./bin/pip --version pip 22.2.2 from /Users/pi/.pyenv/versions/3.10.0/lib/python3.10/site-packages/pip (python 3.10) 

So why isn't this pip getting copied into my .venv when I create it?

I thought that was the way .venv creation worked.

How to clean up my pyenv Python installation so that it spawns up-to-date .venvs?

EDIT:

Insight from #python on IRC/Libera:

grym: I don't think you can; i just get in the habit of python -m venv somevenv && somevenv/bin/python -m pip install --upgrade pip setuptools wheel

jinsun: python -m venv --upgrade-deps .venv is a simple solution if you were just annoying by the pip warning (...) it is updating the pip inside the venv, forget about the base python, I don't even have pip in the base python

2
  • Why not simply upgrade pip once venv is setuped? I assume pip is installed in whatever version was coupled with that particular version Commented Aug 23, 2022 at 18:00
  • That's what I currently do. But I create a lot of .venv-s. It would be cleaner practice to update the reference pip. I just can't see how to do it. Commented Aug 23, 2022 at 21:32

3 Answers 3

3
+50

This is the use case for pyenv-hooks

pyenv-hooks are scripts that are executed by pyenv whenever certain commands are run. You can create hooks for regular commands like: exec, rehash, which, but it can also be a plugin command, like virtualenv. The scripts can be written in any language.

Here is the wiki with official instructions.

You can have a hook by creating a script at the following location:

$PYENV_ROOT/pyenv.d/<hook-name>/<your-script-name> 

For example, to create a hook that upgrades pip, create a new script within this path:

$PYENV_ROOT/pyenv.d/virtualenv/after.bash

With contents:

after_virtualenv 'PYENV_VERSION="$VIRTUALENV_NAME" pyenv-exec pip install --upgrade pip' 

after_virtualenv is the command that tells pyenv when to execute. First, it sets the pyenv version to the name of the virtualenv we just created. with the variable $VIRTUALENV_NAME. Then it upgrades pip itself.

More details in this article.

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

5 Comments

+1 and thanks for the intel. However I'm trying to fill a specific gap in my understanding. AFAIK creating a .venv copies files from a reference Python on the system. And I'm looking to upgrade the pip (and maybe other packages) on this reference Python, so that future spawned .venv-s are up-to-date.
you can try activating the venv and then upgrading pip
That's what I'm currently doing: from the question, "If I execute the suggested command it will upgrade my .venv's pip."
I've been searching everywhere, but I don't see a way to control the default initial pip version. Actually, it is not even the same version I currently have for that python install, it seems to be the fixed version that depends on the os version. What you can do, that is much easier, is to use --upgrade-deps, like this: python3 -m venv --upgrade-deps .venv. It will install pip and upgrade right away.
Rodriguez (SO: why can I not nick-autocomplete/hilight) -- maybe you would care to put this as an answer. As I'm constantly creating .venv-s (to the point I have a shortcut in my .bash_profile for it), I'm going with this solution.
1

Personally I think the real answer here is this, directly upgrading the pip module in the installed python version:

<pyenv root>/versions/<your python version>/bin/python -m pip install -U pip 

Now every time you create a virtualenv like this it will have the latest version of pip

pyenv virtualenv <your python version> <virtualenv name> 

Comments

0

I originally posted it as a comment, but was suggested to make it a proper answer.

An easier approach is to use the upgrade-deps flag when you create a virtual environment. Like this:

python3 -m venv --upgrade-deps .venv 

It was added on python3.9, and according to the official docs:

--upgrade-deps
Upgrade core dependencies (pip, setuptools) to the latest version in PyPI

So, in other words, it will install pip and upgrade right away.

2 Comments

I don't think this actually what the original question was looking for, this is just another way to upgrade the pip in the virtualenv at creation time, similar to the hooks approach. See my answer below for how to direcly upgrade the base python pip
The OP literally told in a comment that this is what worked the best for him, and asked it to be posted as an answer. Maybe it will be useful for other people too :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.