Python Virtual Environments on Raspberry Pi Bookworm
A Beginner’s Guide to Virtual Environments in Python here
A virtual environment is a useful, logical way of isolating your project dependencies from each other and ensuring things don't break when there are conflicts.
As of Raspberry Pi OS bookworm, you'll an error message if you attempt to pip install any Python libraries on your Raspberry Pi.
A virtual environment ensures your Python project dependencies are completely self-contained, and isolated from the system and each other.
It’s a good practice to create a folder on your drive for each main project.
But if you are only doing basic scripts, and want to group them, it’s fine to create only one,
for example: mkdir /home/mark/python Setting Up a Virtual Environment
The syntax to create a virtual environment is simple:
python3 -m venv <name>
python3 -m venv --system-site-packages --prompt myenv ~/my_virtual_env
python3 -m venv Load and run the venv module --system-site-packages
Specifying this argument we're asking "venv" to let our virtual environment see system packages.
--prompt myenv useful for giving each of your virtual environments a unique, friendly name so you can tell them apart.
This might be your project name, or anything memorable to you. It'll show up in your prompt, like so: ~/my_virtual_env the location you want to put your virtual environment.
This could be anywhere but in most cases you might want it in your project directory.
To use your virtual environment you must first activate it: source ~/my_virtual_env/bin/activate
Pimaroni article here