pippython-venvpyenvpyenv-virtualenv

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


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


Solution

  • 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.