When trying to create a virtual env using venv
for Python, a version of Python that is already installed system-wide must be used, but a version of the venv
library from the system must also be used. These are two pre-requisites for setting up a virtual environment.
WARNING: I did this in a hurry because I needed it but please be warned: this may break your system Python with a result that applications that rely on it may break.
The problem I had, matching symptoms given here, seems to be that when trying to create a venv
using Python 3.10.8, the venv
module for Python 3.8.10 was being used.
So, given Python 3.8 and Python 3.10 are already installed using apt
, first of all I uninstalled these packages:
sudo apt purge python3-venv python3.8-venv
sudo apt autoremove
Then I linked python3
to point to Python 3.10:
cd /usr/bin
sudo rm python3
sudo ln python3.10 python3
Then I installed the venv
for Python3.10:
sudo apt install python3.10-venv
This now means creating a virtual environment for Python 3.8 doesn't work (because Python3.8 venv
has just been removed). I'm not sure if there is a means to have them both working, and I haven't yet tried to just install python3.8-venv
again and try them both, as I need my 3.10 environment working quickly, right now ;-). But it seems possible there has been some conflict introduced when following the usual upgrade route within Ubuntu 20.
However, venv
for Python 3.10 should now work as expected:
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -V
Python 3.10.8
(.venv) $ pip install --upgrade pip
...
(.venv) $ pip list
Package Version
---------- -------
pip 22.3
setuptools 63.2.0