Field Notes: Environments
Most "Python packaging is broken" stories are really "the package went into a different environment than the one that ran the code". These notes are the habits that prevent that, collected from support threads and incident reports.
Never sudo pip install
sudo pip install x runs pip as root against the system interpreter. It can overwrite files that the OS package manager owns (on Debian/Ubuntu, /usr/lib/python3/dist-packages belongs to apt), install a newer requests that an OS tool wasn't tested with, and leave root-owned files in your home directory's caches. Distributions now block it with PEP 668's EXTERNALLY-MANAGED marker, and the escape hatch --break-system-packages is named honestly.
| You want to… | Do this instead |
|---|---|
| use a library in a project | a venv per project (python -m venv .venv, uv venv, uv sync, poetry install) |
| install a CLI app for yourself | uv tool install x or pipx install x |
| run a script with dependencies once | uv run --with x script.py, or PEP 723 inline metadata |
| install a library for system tools | the distribution's package (apt install python3-requests) |
| a server's application | a venv owned by the service user, e.g. /opt/myapp/.venv |
The --user trap
pip install --user installs into ~/.local/lib/python3.X/site-packages, which every run of that Python version sees. It was the old recommendation for avoiding sudo, and it causes subtle bugs: a stale --user copy of a package silently shadows the system one for every script you run, but it's invisible inside venvs. Today it's blocked in both places it used to work:
python3 -m site --user-site
echo "--- outside a venv: PEP 668"
pip install --user attrs 2>&1 | head -1
echo "--- inside a venv: pointless, so refused"
python3 -m venv .venv
.venv/bin/pip install --user attrs 2>&1/home/learner/.local/lib/python3.13/site-packages
--- outside a venv: PEP 668
error: externally-managed-environment
--- inside a venv: pointless, so refused
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
If you inherit a machine where --user installs exist, python3 -m pip list --user shows them. Remove them (pip uninstall as the same user) unless you know why they're there.
Which pip is this?
pip on your PATH and python on your PATH can belong to different interpreters (a Homebrew Python and a pyenv one, a forgotten venv in PATH, Windows' Store alias). The fix is to always run pip through the interpreter you mean:
python3 -m venv app
python3 -m venv other
. other/bin/activate
pip install -q attrs==26.1.0
echo "--- installed with the active venv's pip, run with another interpreter"
app/bin/python -c 'import attrs' 2>&1 | tail -1
echo "--- python -m pip makes the target explicit"
app/bin/python -m pip install -q attrs==26.1.0
app/bin/python -c 'import attrs; print("attrs", attrs.__version__, "in", attrs.__file__.split("/")[3])'
pip --version | sed 's/ from .*//'; command -v pip--- installed with the active venv's pip, run with another interpreter
ModuleNotFoundError: No module named 'attrs'
--- python -m pip makes the target explicit
attrs 26.1.0 in app
pip 26.2.2
/home/learner/other/bin/pip
Rules that follow:
- Write
python -m pip install …in docs and scripts, notpip install …. - In scripts and services, call
/path/to/.venv/bin/pythonexplicitly; never rely on activation. - In Jupyter, use the
%pip install xmagic: it installs into the kernel's interpreter, which is often not the one your terminal'spipbelongs to. - When something "is installed but can't be imported", print
sys.executableandpython -m pip --version. They'll disagree.
Containers
- A venv inside a Docker image is still worth it: it separates your app's packages from the base image's Python tools, and lets a multi-stage build copy one directory (
COPY --from=build /app/.venv /app/.venv). - Official
python:images don't mark their Python externally managed, sopip install"works" there. Distribution images (debian,ubuntu) do. - Build the venv in the same image (same Python path) you run it in: venvs aren't relocatable across interpreters.
Small habits that save hours
- One
.venvper project, in the project root, ignored by Git. Editors, uv and Poetry (with in-project venvs) find it. - Recreate rather than repair. With a lockfile,
rm -rf .venv && uv synctakes seconds and removes every "it worked yesterday" variable. - Don't mix installers in one environment.
uv syncwill remove whatpip installadded;poetry installwon't know about it. Add dependencies through the project tool. - Keep
PYTHONPATHempty. It's added to every interpreter, venvs included, and is the most common source of "wrong version imported".
Test yourself
You need the httpie CLI on your laptop. What's the best way to install it?
- sudo pip install httpie
- pip install --user httpie
- uv tool install httpie (or pipx install httpie)
- pip install --break-system-packages httpie
import attrs fails although you just ran pip install attrs. What do you check first?
- Reinstall Python
- Whether
pipand thepythonrunning the code are the same interpreter: compare sys.executable withpython -m pip --version - Delete __pycache__
- Upgrade pip
Why is pip install --user refused inside a virtual environment?
- Permissions
- The user site-packages directory isn't visible in a venv, so the install would be pointless
- It's deprecated
- venvs are read-only