Python Best Practices

Chapter 1 · Practice 2 of 5

Give every project its own virtual environment

From Versions, environments and project setup

Why it matters

Installing packages into the system Python mixes the dependencies of every project on the machine, and on many Linux distributions the system Python belongs to the operating system. A virtual environment per project keeps versions separate and makes "works on my machine" problems visible.

  • With the standard library: python -m venv .venv, then activate it.
  • With uv: uv sync creates .venv and installs the locked dependencies in one step.

Run tools through the environment (python -m pytest, uv run pytest) so you never pick up a global copy by accident.

Example

Shell
python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
python -m pip install -e .

# or with uv
uv sync
uv run pytest