EngineeringPython

Fixing pip's externally-managed-environment Error

Python 3.11 refused the install because the environment is externally managed. That is a deliberate protection, and there are three ways past it.

Kushan Manahara

March 6, 2023 · 2 min read

0683
Fixing pip's externally-managed-environment Error

When I tried to install Pipenv on Python 3.11.2, the install failed with an externally-managed-environment error.

The message means the Python environment is externally managed, so you cannot install packages system-wide with pip. This is not a bug, and it is not the distro being awkward. It comes from PEP 668, and it exists because pip and your system package manager both write to the same directories. When pip overwrote a file that apt or dnf depended on, things broke later, in ways that were nearly impossible to trace back.

The block is triggered by a real marker file, which is worth knowing about because it tells you exactly which interpreter is protected:

terminal.sh
ls /usr/lib/python3.11/EXTERNALLY-MANAGED
cat /usr/lib/python3.11/EXTERNALLY-MANAGED

The contents of that file are what pip prints at you. Distributions use it to point you at their preferred fix.

For a project: a virtual environment

A virtual environment is a private copy of Python's package directory that belongs to your project rather than the OS. Nothing the system depends on lives inside it, so pip is free to write there.

terminal.sh
python3 -m venv myenv
source myenv/bin/activate

The environment is now active and pip installs into it rather than system-wide:

terminal.sh
pip install pipenv

When you are done, deactivate it. And if you want it gone entirely, delete the directory, because that is all a venv is:

terminal.sh
deactivate
rm -rf myenv

For a command-line tool: pipx

Pipenv is a tool you run, not a library you import, so there is a better fit. pipx creates a dedicated virtual environment for each application and puts the command on your PATH, so you get the tool without managing an environment for it.

terminal.sh
sudo apt install pipx
pipx install pipenv
pipx ensurepath

This is the right default for anything you want available system-wide as a command.

The override, and when not to use it

There is a flag that skips the check entirely:

terminal.sh
pip install --break-system-packages pipenv

The name is honest about what it does. It is reasonable inside a throwaway container or CI job where the whole filesystem is discarded afterwards. On a machine you actually use, it can break your OS package manager months later, long after you have forgotten you ran it. Reach for the venv or pipx instead.

Written by

Kushan Manahara

Responses (0)

Verified name, role, and email required before posting.

No responses yet

Be the first to share your thoughts, benchmarks, or feedback above.