venv (Python Virtual Environment)

An isolated directory containing a standalone Python installation and packages, separate from the system Python.

Why: from Debian Bookworm onwards, pip install system-wide is blocked (externally-managed-environment error). venvs are the required solution.

Benefits: avoid system pollution, avoid version conflicts between projects, reproducible environments via requirements.txt.

Non-portable: venvs use absolute paths internally. Don’t move the .venv folder — recreate with pip install -r requirements.txt.

python -m venv .venv              # create
source .venv/bin/activate         # activate
pip install <package>             # install locally
deactivate                        # deactivate

sudo + venv: sudo python uses root’s Python, not the venv. Fix:

sudo $(which python) script.py   # while venv is active

See Also