Starting with the October 10, 2023 Bookworm release of the Raspberry Pi OS, the use of Python Virtual Environments (venv) when pip installing packages is required. No more sudo pip. This will break things and require learning new things. Yeah.
This guide tries to help with this transitional pain by covering basic venv usage on Raspberry Pi's.
Why This Change?
This change is being done to protect the system level Python installation. This is not a new issue and using venv has long been the recommended solution. We've just been slow to adopt this on Raspberry Pi boards.
The main issue is this - when Python modules are installed using pip
, they get installed into the system level Python installation. This can potentially break the system level installation.
Read the PEP for more details:
Using a virtual environment, the pip installed packages get placed there instead of at the system level.
Nomenclature
In this guide, venv is used as short hand for Python Virtual Environment in general. It's also the name of the Python module used to create new Virtual Environments.
Python "modules" may also be referred to as libraries or packages. They're the thing that gets install via pip.
Also, "Python" here always means Python 3. Code examples may show either python
or python3
. The commands should be equivalent.
Installing venv
Raspberry Pi OS's should already have Python installed. However, the venv module itself may or may *not* be included as part of the OS image along with the Python install. If you get errors when running the venv setup, then you may need to install the venv module:
sudo apt install python3-venv
Text editor powered by tinymce.