Environments Best Practices
Practical rules for keeping Python environments isolated, reproducible, and team-friendly.
Search across all documentation pages
Practical rules for keeping Python environments isolated, reproducible, and team-friendly.
.python-version or pin in pyproject.toml. Everyone runs the same interpreter..venv/ to .gitignore. Environments are reproducible from lockfiles, not copied.uv.lock, poetry.lock, or pdm.lock). CI and prod install identical packages.uv sync --frozen (or equivalent) in CI. Builds fail if the lockfile is stale.requires-python in pyproject.toml. Block installs on unsupported interpreters.src/ layout with editable installs. Tests import the installed package, not the repo root.pyproject.toml. One file for metadata, deps, ruff, pytest, mypy.uv for new projects. Fast installs, built-in lockfile, Python version management.~/.netrc.pip-audit or uv pip audit in CI weekly.uv lock after any dependency change. Keep pyproject.toml and lockfile in sync.requirements.txt only for Docker legacy stages. The lockfile is the source of truth.Libraries publish version ranges. Applications and services must commit lockfiles.
uv for speed and simplicity. Poetry if the team already has mature Poetry workflows and training.
Create pyproject.toml, import deps with uv add or pdm import, generate a lockfile, delete requirements.txt.
__pycache__/, *.egg-info/, .mypy_cache/, .ruff_cache/, dist/, .env.
Cache the venv keyed on the lockfile hash. Recreate when the hash changes.
Use environment markers in pyproject.toml: sys_platform == 'win32'.
No. Isolation prevents version conflicts between projects.
When security support ends for your current version or when you need new language features. Test in a branch first.
.env.example with dummy values in the repo. Real .env gitignored. CI uses secret stores.
In Docker, install deps globally in the container (no venv needed). On the host, always use a venv.
Stack versions: This page was written for Python 3.14.0, FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 19, 2026