Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

error: externally-managed-environment (pip): how to fix it

error: externally-managed-environment means pip found an EXTERNALLY-MANAGED marker in the Python it was about to install into: that Python belongs to your operating system's or Homebrew's package manager (PEP 668), and pip will not change it outside a virtual environment. Install into a virtual environment instead (python3 -m venv .venv), use pipx for command-line tools, or build on the official python Docker image; --break-system-packages is the last resort.

By Livops team, the people who build and run LivopsUpdated

pip ends with this when the Python you run it with is managed by something else. The lines are pip’s own[3]; the text after ╰─> comes from the Python’s distributor, here the opening of Debian’s message as PEP 668 gives it[1]:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    ...

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

On another distributor’s Python the text under ╰─> is whatever its marker file says; when the marker gives none, pip prints its default, which starts The Python environment under /usr is managed externally with your Python’s prefix in place of /usr[3]. Same error, same fixes. This page explains the marker file behind it, gives the fixes in the order we would try them, and says what Livops does.

What does the error mean?

PEP 668 lets a distributor mark a Python installation as externally managed: its packages belong to a package manager such as apt or Homebrew, not to pip[1]. The mark is a file named EXTERNALLY-MANAGED in the standard library directory, the one sysconfig.get_path("stdlib", sysconfig.get_default_scheme()) returns[2]. You can see it yourself:

python3 -c 'import sysconfig; print(sysconfig.get_path("stdlib", sysconfig.get_default_scheme()))'
ls "$(python3 -c 'import sysconfig; print(sysconfig.get_path("stdlib", sysconfig.get_default_scheme()))')"/EXTERNALLY-MANAGED

An installer refuses only when both are true: it is running outside a virtual environment, and the file exists[2]. Inside a virtual environment the marker does not apply; the check is whether sys.prefix equals sys.base_prefix[1]. pip has read the marker since pip 23.0[6].

Why distributions do this

The PEP’s reason is conflicts between the operating system’s package manager and Python tools like pip: a pip install into the system Python can replace or remove files the distribution owns and break its own tools written in Python[1]. So the system Python is left to the system, and your packages go somewhere that is yours.

Who ships the marker:

  • Debian declares its Python externally managed, so pip refuses to install system-wide, and installing into your home directory with pip install --user is disabled by the same policy[7].
  • Homebrew marks its current Python as externally managed and says to install project dependencies in a virtual environment instead of its base environment[8].

Fix 1: install into a virtual environment

This is the fix the specification itself points to: an installer’s default message should suggest creating a virtual environment[2]. In the project folder:

python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
.venv/bin/python main.py

You do not need to activate the environment: running its own Python by path is enough, and scripts installed into it run without activating[9]. To work in it interactively, source .venv/bin/activate[9].

On Debian, the venv module comes in the separate python3-venv package[7]:

sudo apt install python3-venv

Keep .venv out of git. A virtual environment is not movable or copyable; you recreate it where it runs, and since Python 3.13 venv writes a .gitignore into it for you[9].

Fix 2: pipx for command-line tools

If what you are installing is a program, not a library for your code (black, httpie, yt-dlp and the like), use pipx. It creates a virtual environment for each package and puts its commands in a directory on your PATH[10]. Debian recommends it for Python applications it does not package[7], and Homebrew for command-line applications that should each have an isolated environment[8]:

brew install pipx        # macOS with Homebrew
pipx install black

On Linux, install pipx from your distribution’s package manager.

Fix 3: the official python image in Docker

The error in a Dockerfile usually means the image got its Python from the distribution, for example FROM debian and apt-get install python3-pip; then the distribution’s marker comes with it. The official python images compile CPython from source, put it on /usr/local/bin, and their Dockerfile has no step that writes an EXTERNALLY-MANAGED file[11]. Build on one of them:

FROM python:3.12-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]

If you must stay on a distribution image, create a virtual environment in it and install there, as in Fix 1. PEP 668 also covers this case: a distributor may leave the marker out of a single-application image that is not updated after it is built, but is advised to keep it in images where users install package updates[1][2].

Last resort: –break-system-packages

pip has an override, as the specification asks installers to have, off by default and named to signal the risk[2]. --break-system-packages lets pip modify an externally managed installation[5]; it arrived in pip 23.0.1[6].

pip install --break-system-packages requests

pip’s own message offers it “at the risk of breaking your Python installation or OS”[3], and Debian says you do it at your own risk, since pip may break the Python modules Debian packages[7]. Like any pip option it can also be set as the variable PIP_BREAK_SYSTEM_PACKAGES=1[12][7], which turns it on for every pip command that sees the variable. Use it in a throwaway container or CI job, never on a server you care about.

Two things that are not fixes:

  • pip install --user is checked like a system install: pip skips the check only for --target, --prefix and --root installs[4], and on Debian user installs are disabled by the same policy[7].
  • Deleting the EXTERNALLY-MANAGED file is the same risk as the flag, made permanent for everything that uses that Python. Use a virtual environment instead.

How to confirm the fix locally

Check that the pip you run is the environment’s, not the system’s:

.venv/bin/python -m pip --version
.venv/bin/python -c 'import sys; print(sys.prefix != sys.base_prefix)'

The first line names a path inside .venv, and the second prints True inside a virtual environment[1]. Then install your requirements with that pip; with the marker out of the way, any error left is about the packages themselves.

On Livops

  • You do not install into a system Python. Livops installs your dependencies itself, and the commands run from the project’s .venv: a start command you write in Settings → Start begins with .venv/bin/, for example .venv/bin/python main.py, and a Procfile line reads worker: .venv/bin/python bot.py.

  • Do not upload your .venv. When you upload a folder, venv and .venv are dropped automatically; we install dependencies ourselves.

  • Your own Dockerfile. The Dockerfile is used by default when no framework was recognised; to build a recognised Python project from it instead, add "builder": "dockerfile" to livops.json before the project is created. If it installs Python with apt, start it FROM python:3.12-slim instead (Fix 3), or install into a virtual environment inside the image.

    {
      "builder": "dockerfile"
    }
  • When an install fails, the deploy stops as install_failed, the build log shows pip’s output, and the old version keeps running. Next to the failed build, the assistant’s button reads the log and explains it.

  • Choose the Python. Livops runs Python 3.10 to 3.14. A .python-version file at the project root pins it:

    3.12

A Python web app costs 39 credits a month; a bot, worker or cron job 19. When pip cannot find a package at all, see No matching distribution found; the install errors are in Install failed.

Questions

Is this a bug in pip?

No. pip is following PEP 668: the Python you are installing into carries an EXTERNALLY-MANAGED marker file, which says its packages are managed by another tool, such as apt or Homebrew. pip refuses on purpose and points you at a virtual environment.

Why did pip install work before and stop now?

pip has read the marker since pip 23.0. If the same command used to work, either your pip was older than that, or the Python you installed into had no EXTERNALLY-MANAGED file at the time.

Does pip install --user avoid the error?

No. A --user install still goes into the externally managed interpreter's environment, so pip checks the marker and refuses. Debian's own notes say user installs are disabled by the same policy. Use a virtual environment.

Is --break-system-packages safe?

It does what its name says. pip's own message offers it at the risk of breaking your Python installation or OS, and Debian warns pip may break the Python modules it packages. Use it only in a throwaway container or a machine you are willing to rebuild.

Do I get this error inside a virtual environment?

No. The marker applies only outside a virtual environment. If you still see it, the pip you ran is not the one in the environment: call .venv/bin/python -m pip, or activate the environment first.

Does the official python Docker image have this error?

Its Python is compiled from source into /usr/local, and its Dockerfile writes no EXTERNALLY-MANAGED marker, so pip install works there. Images that install Python from the distribution, such as apt-get install python3-pip on a Debian image, bring the marker with them.

Which Python versions does Livops run?

Python 3.10 to 3.14. Put the version in a .python-version file at the project root, for example 3.12.

Sources

  1. PEP 668 — Marking Python base environments as "externally managed" (peps.python.org). Checked .
  2. Python Packaging User Guide — Externally Managed Environments (packaging.python.org). Checked .
  3. pip source — src/pip/_internal/exceptions/__init__.py (ExternallyManagedEnvironment) (raw.githubusercontent.com). Checked .
  4. pip source — src/pip/_internal/commands/install.py (raw.githubusercontent.com). Checked .
  5. pip docs — pip install (pip.pypa.io). Checked .
  6. pip docs — Changelog (pip.pypa.io). Checked .
  7. Debian python3 package — README.venv (salsa.debian.org). Checked .
  8. Homebrew docs — Language runtimes and packages (docs.brew.sh). Checked .
  9. Python docs — venv (docs.python.org). Checked .
  10. Python Packaging User Guide — Installing stand alone command line tools (packaging.python.org). Checked .
  11. docker-library/python — Dockerfile-linux.template (raw.githubusercontent.com). Checked .
  12. pip docs — Configuration (pip.pypa.io). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Python app

Got a question? — @livops_uz