How to upgrade pyenv (macOS) such that a fresh .venv contains an up-to-date pip?

Viewed 117

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

1 Answers

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.

Related