Raspberry Pi OS manages Python packages a bit differently to some Linux installs and will insist on using a virtual environment (e.g. you get an error: “error: externally-managed-environment”)

Creating a virtual environment is good practice anyway for Python development in general, as it helps to manage dependencies and avoid conflicts.

Update system and ensure virtualenv is installed

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install virtualenv

Create a new virtual environment

Virtual environment name

“venv” is often used, but it doesn’t matter, it can be anything

Navigate to where you want it to be stored

Navigate to the directory within which you want the virtual environment to be created. For example:

cd /home/myfolder
Create the virtual environment

NOTE: The virtual environment will be created using whatever version of Python you create it using.
If you just use “python3”, it will be the system python version.
If you have other versions installed you can instead specify the path to them (e.g. “/usr/bin/python3.8”) so that version version of Python is used instead (useful if your system OS python version is old and you have installed a newer version you want to use for the new virtual environment)

Create a virtual environment called “venv”:

python3 -m virtualenv venv --system-site-packages

The “–system-site-packages” is an optional argument which means that the virtual environment will also have access to the system site-packages directory (see here).

The venv/ folder will be created and all of the requried python files added into it.

Create an app/ folder

You don’t have to, but its a good idea to create a directory within it for your app/apps to go in:

mkdir venv/app
chmod 0755 venv/app

Using the virtual environment, method 1 – Via the command line and in scripts

Activate the virtual environment so you can use it (needs to be done each time the venv is used)
source venv/bin/activate
Use the virtual environment (run your Python code here)

Once the virtual environment has been activated Python is used as normal. Running python or pip will be done in the context of the virtual environment. Modules installed with pip will be placed in the local venv folders (sudo should not be used).

Install a package wihin it using pip
/home/myfolder/venv/bin/pip3 install mypackagename
Run your app

You can copy your app files into the virtual environment if you want, for instance into a folder called \app.

Running it from the command line after activating the virtual environment:

python3 venv/app/myappname.py

Deactivate the virtual environment (optional)

deactivate

Using the virtual environment, method 2 – from exec commands etc

Create the virtual environment

(Same as before, but you can do this from exec commands etc too if you specify full paths):

/usr/bin/python3 -m virtualenv /home/myfolder/venv --system-site-packages
mkdir /home/myfolder/venv/app
chmod 0755 /home/myfolder/venv/app
Activate the virtual environment so you can use it

You don’t need to, instead you’ll specify the path to the virtual environments python binary

Install a package wihin it using pip
/home/myfolder/venv/bin/pip3 install mypackagename
Run your app
/home/myfolder/venv/bin/python3 /home/myfolder/venv/app/myappname.py

Delete a virtual environment

As long as nothing is running within it, just delete the venv/ folder.

More info on virtual environments

https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi/automatically-running-at-boot

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *