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

P i
  • 29,020
  • 36
  • 159
  • 267
  • Why not simply upgrade pip once venv is setuped? I assume pip is installed in whatever version was coupled with that particular version – JFCorleone Aug 23 '22 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. – P i Aug 23 '22 at 21:32

2 Answers2

3

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.

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
  • +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. – P i Aug 24 '22 at 15:52
  • you can try activating the venv and then upgrading pip – Rodrigo Rodrigues Aug 24 '22 at 15:59
  • That's what I'm currently doing: from the question, "If I execute the suggested command it will upgrade my .venv's pip." – P i Aug 24 '22 at 16:16
  • 1
    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. – Rodrigo Rodrigues Aug 25 '22 at 01:57
  • 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. – P i Oct 03 '22 at 17:58
1

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.

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36