Skip to content

Fix pip's Externally Managed Environment on Raspberry Pi

If pip install fails with error: externally-managed-environment on Raspberry Pi OS Bookworm or Trixie, create a virtual environment in your project and run pip with that environment's Python. For projects that also use a GPIO or camera library installed through APT, create the environment with access to those system packages.

1. Create a Python virtual environment on Raspberry Pi

Raspberry Pi OS manages its system Python and libraries through APT. The marker that triggers this pip error protects the Python installation used by OS packages. A project virtual environment gives pip a separate installation directory. Raspberry Pi OS Python documentation, Python packaging specification

Install virtual environment support if you need it.

sudo apt update && sudo apt install python3-venv

For a practice project, work in a new directory. For existing code, run python3 -m venv .venv from its project directory if it does not already have a working virtual environment.

mkdir ~/pi-python-demo && cd ~/pi-python-demo && python3 -m venv .venv

If the practice directory already exists, choose a new name. Run the rest of the commands from inside the project.

2. Install a package and check which Python runs it

The path .venv/bin/python picks the project interpreter each time. Use it for both installing and running the package. This example installs humanize, a library that formats values such as file sizes.

1
2
3
.venv/bin/python -m pip --version
.venv/bin/python -m pip install humanize
.venv/bin/python -c 'import humanize; print(humanize.naturalsize(2000))'

The first command should show a pip path inside .venv. The last command should print 2.0 kB. To install a project's existing dependency list, use .venv/bin/python -m pip install -r requirements.txt from the directory that contains the file.

If you prefer shorter commands during an interactive session, activate the environment in Bash. This changes command lookup for that shell. Once activated, run python -m pip for installs and python your_script.py to run the project. Python venv documentation

1
2
3
source .venv/bin/activate
python -m pip --version
deactivate

For a systemd service, use the absolute path to the environment's Python in ExecStart=, for example /home/alex/pi-python-demo/.venv/bin/python. Use the same interpreter when testing the program manually.

If the service reports status=203/EXEC, check that systemd can launch that interpreter. Fix systemd status=203/EXEC

3. Keep access to an APT-installed GPIO or camera library

A standard virtual environment hides third-party packages installed for the system interpreter. If your project uses an APT-provided library such as GPIO Zero or Picamera2 alongside dependencies from pip, create a separate environment with --system-site-packages.

1
2
3
sudo apt install python3-gpiozero
python3 -m venv --system-site-packages .venv-gpio
.venv-gpio/bin/python -c 'import gpiozero; print(gpiozero.__file__)'

The import checks that Python can find the library. It requires no GPIO wiring. Use .venv-gpio/bin/python -m pip for additional project packages. This type of environment exposes the OS's Python packages, so use it for projects that need them and keep updates to APT-provided libraries under APT. For camera projects, see the Picamera2 setup and capture test.

On a Pi 4B with Raspberry Pi OS Trixie and Python 3.13, system pip reported externally-managed-environment. The project environment installed and imported humanize, while the GPIO environment imported gpiozero from /usr/lib/python3/dist-packages.

If pip or the import still fails

What you see What to check
externally-managed-environment again Check the path reported by python -m pip --version, or use .venv/bin/python -m pip explicitly. Keep pip installs in the environment under your own account.
ensurepip is not available when creating the environment Install python3-venv for the Raspberry Pi OS interpreter and recreate the environment.
ModuleNotFoundError after an install Run the script with the same interpreter you used to install the library. For a GPIO or camera package from APT, check whether the project needs the environment from step 3.
A copied environment stopped working after an OS upgrade Create a fresh environment on the new system and install from the project's dependency list. Bookworm to Trixie migration
Donate