Python Virtual Environment Setup on Arch Linux

pythonvirtualization

I am a big fan of the rolling distribution model. I do need help with one hiccup. when I create a virtual environment in python to isolate a project all works great until my rolling distro upgrades python versions.

When this happens my python project no longer works, so I destroy my venv and create a new one. It seems like I shouldn't have to be this drastic in my approach. It takes a while and as the number of projects grows this doesn't scale well.

Here's how I'm creating my venvs:

$ python -m venv venv
$ source venv/bin/activate
$ command -v python
$ python -m pip install --upgrade pip
$ python -m pip install django     # or whatever
$ python --version && python -m django --version
  1. am I creating my venv wrong?
  2. if upgrading the system's python breaks my venvs, is there an elegant way to fix rather than just recreating the venv?

Best Answer

Let's assume a venv was created with python3.9, right before python3.10 was released. venv will create a bin directory symlinked to the system python, as in venv/bin/python3 -> /usr/bin/python3 This unversioned python binary itself might not a problem. However, the site-packages directories are major version specific, which makes your python3.9 projects no longer work.

I consider venvs an immutable set of a given python version and installed libraries. When major changes happen, destroy and recreate seems reasonable. They are lightweight. Of course I have not had to do this repeatedly like you have. Consider improving your automation such that you can rebuild all of these at once on demand.

Arch is not interested in maintaining a python for a very long time, based on their rolling distro concept. Maybe you can find and are comfortable with a user contributed package maintaining say python39 on AUR, but maybe not. Consider switching to a long term support distro so you have more time on a given major version of Python. Could be confined to containers or whatever, doesn't need to replace all you like about Arch.