Python venv brittleness (with fix?)

I just upgraded from Ubuntu 22.10 to 23.04. Which means I upgraded from Python 3.10 to 3.11. Which means all my old Python programs don’t work anymore. Because each has a virtual environment and the venv is hardcoded to Python 3.10.

Am I doing it wrong? Yes, I was doing it wrong.

In the old days I’d create a venv with a command like python3.10 -m venv venv. This results in a venv/bin directory where python3 is a symlink to python3.10 in the same directory, which in turn is a symlink to /usr/bin/python3.10 (which no longer exists).

Awhile back I changed to running python3 -m venv venv. This results in all the binaries in venv/bin being symlinks to python3, which in turn is a symlink to /usr/bin/python3. That should be robust to upgrades. And Python is good about backward compatibility so using 3.11 to run a venv built for 3.10 should mostly be fine.

FWIW the docs just have you invoking python on the command line, so I guess that’s what the intended usage was. More generally it looks like venv is doing its best to set up an environment with the exact same Python pathname you used to create the environment, which seems reasonable enough.

One thought on “Python venv brittleness (with fix?)

  1. I like using pipx for all of my Python utilities because then I can run “pipx reinstall-all” to have it rebuild every venv used by every CLI tool I use when a new Python version comes out. It’s enough of a workflow improvement that I hope it goes into the stdlib at some point.

Comments are closed.