TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › Field Notes: Environments

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:

bash · try it yourself
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:

bash · try it yourself
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, not pip install ….
  • In scripts and services, call /path/to/.venv/bin/python explicitly; never rely on activation.
  • In Jupyter, use the %pip install x magic: it installs into the kernel's interpreter, which is often not the one your terminal's pip belongs to.
  • When something "is installed but can't be imported", print sys.executable and python -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, so pip 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 .venv per 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 sync takes seconds and removes every "it worked yesterday" variable.
  • Don't mix installers in one environment. uv sync will remove what pip install added; poetry install won't know about it. Add dependencies through the project tool.
  • Keep PYTHONPATH empty. 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?

  1. sudo pip install httpie
  2. pip install --user httpie
  3. uv tool install httpie (or pipx install httpie)
  4. pip install --break-system-packages httpie

import attrs fails although you just ran pip install attrs. What do you check first?

  1. Reinstall Python
  2. Whether pip and the python running the code are the same interpreter: compare sys.executable with python -m pip --version
  3. Delete __pycache__
  4. Upgrade pip

Why is pip install --user refused inside a virtual environment?

  1. Permissions
  2. The user site-packages directory isn't visible in a venv, so the install would be pointless
  3. It's deprecated
  4. venvs are read-only