In this article
Fix pip's “externally managed environment” error by installing project dependencies in a virtual environment rather than modifying the protected base Python. First make sure the interpreter running pip is the one you intend to use.
The Python packaging specification describes the marker that tells packaging tools an environment is managed externally. The error is a protection boundary, not proof that the package you requested is broken.
Create a project environment#
From your project directory on a typical Unix-like shell:
python3 -m venv .venv
.venv/bin/python -m pip install requestsrequests is an example dependency; replace it with the package your project requires. The explicit interpreter path avoids ambiguity about which pip receives the installation.
On Windows, an equivalent example is:
py -m venv .venv
.venv\Scripts\python.exe -m pip install requestsThese are documentation-based examples, not commands tested on your machine. Check your installed Python and shell if the launcher differs.
Understand what activation does#
Python's venv documentation explains virtual environments and platform-specific activation. Activation changes how your shell resolves commands; you can also call the environment's interpreter directly.
For a Unix-like shell, activation commonly uses:
source .venv/bin/activate
python -m pip install requestsIf a command still uses the system environment, return to the explicit path. Do not interpret a prompt prefix alone as conclusive proof of interpreter identity.
Check which interpreter is running#
Use the project environment directly:
.venv/bin/python -c "import sys; print(sys.executable); print(sys.prefix != sys.base_prefix)"
.venv/bin/python -m pip --versionThe first command identifies the executable and checks the virtual-environment relationship described in Python's documentation. The second shows pip's location.
On Windows, use .venv\Scripts\python.exe in place of .venv/bin/python.
If these point somewhere unexpected, inspect your environment creation, shell aliases and editor configuration. Installing into one interpreter and running another is a common reason a package appears missing afterward.
If creating the environment fails#
Read the exact error. Some managed distributions require an additional distribution-provided component for venv support. Follow your operating-system package manager's instructions rather than trying to repair the system interpreter with arbitrary pip commands.
If Python was installed through another managed tool, use that tool's documented environment workflow. Avoid mixing multiple installation managers without knowing which owns the executable.
Distinguish applications from libraries#
A library belongs in the environment of the project that imports it. A standalone command-line application may have a documented isolated installer or an operating-system package.
Choose the distribution route that matches the tool. You do not need to put every command-line application into the same project environment.
Pip's interpreter-selection documentation provides another supported way to target an environment explicitly.
Avoid bypassing the protection as a first fix#
An override flag can change pip's behavior without solving the ownership problem. Do not remove the management marker or use elevated global installation simply to silence the error.
Keep dependency declarations in the project and recreate environments when appropriate. Do not commit a machine-specific .venv directory as a portable installation.
For other development-environment maintenance, see safe Docker disk cleanup. When an assistant supplies installation commands, use our AI-generated code review guide to check scope and consequences.