Reproducible Python environments with uv

A test passes on your Mac, fails in CI, and the traceback points at code nobody changed. The usual cause is not mysterious Python behavior. It is an environment that was allowed to change underneath the project. Reproducible Python environments make that change visible, controlled, and reviewable.
This is not only about getting the same version of one library. Python projects depend on an interpreter, direct dependencies, transitive dependencies, platform markers, build tools, and sometimes system libraries. A useful setup records the parts that matter, installs them predictably, and refuses to silently improvise during a build.
A lockfile is necessary, not sufficient
A pyproject.toml declares intent. It might say that a project needs httpx>=0.27 or ruff>=0.6. That is appropriate for a library that must work across a version range, but it is not a complete deployment record. A fresh install can select newer releases tomorrow, including different transitive packages.
A lockfile records the resolved graph: the exact package versions, source distributions or wheels, hashes where supported, and dependency relationships selected at resolution time. For an application, commit uv.lock with pyproject.toml. Review lockfile changes as real code changes, because they can alter behavior, licensing, and CVE exposure without touching application code.
That said, a lockfile cannot pin the Python interpreter by itself. It also cannot make a Linux-only wheel run on macOS, or supply a missing compiler library. Reproducibility is a stack of constraints, not a single file.
Define what must stay the same
Before choosing commands, decide what “reproducible” means for the project. There are at least three useful levels.
At the development level, every contributor should get the same interpreter and resolved Python dependencies. This eliminates most machine-specific surprises. At the CI level, the test runner should use the same locked graph and fail if the declared dependencies and lockfile have drifted apart. At the release level, you may also need a fixed OS image, compiler toolchain, environment variables, and external service versions.
Do not promise byte-identical artifacts unless you actually control all of those inputs. Native extensions, timestamps, platform wheels, and compiler flags make that a stricter problem. Most teams need behavioral reproducibility: the application installs and runs the same way from the recorded inputs on a supported platform.
Pin the interpreter deliberately
Python 3.12 is not one interpreter. Python 3.12.0 and 3.12.7 can differ in bug fixes, bundled components, and build behavior. A minor-version pin may be sufficient for a library tested across a range. For an application, a patch-version pin is usually the cleaner default.
With uv, place the interpreter decision in the repository:
uv python pin 3.12.7
This creates or updates .python-version. Commit it. In CI, install or select that same version rather than relying on whichever Python happens to be preinstalled on the runner.
Keep the project metadata honest, too. requires-python describes the versions your package supports. The .python-version file describes the interpreter used for day-to-day development and a specific application build. Those are related but not interchangeable.
Declare direct dependencies, then lock the graph
Add dependencies through the project tool instead of manually editing a requirements file after the fact:
uv add httpx uv add --dev pytest ruff uv lock
The result belongs in version control: pyproject.toml, uv.lock, and .python-version. The virtual environment does not. It is an installation result, not the source of truth.
Avoid maintaining two competing dependency definitions unless you have a concrete packaging requirement. A hand-maintained requirements.txt plus a lockfile plus a loose dependency list creates three places for drift. If another system requires an exported requirements file, generate it from the locked project as part of a controlled build process.
Build reproducible Python environments from the lock
The install command matters as much as the files you commit. A command that resolves again during every CI run can hide an outdated lockfile. A command that ignores the lock can turn a green build into a moving target.
For local setup and CI, use the lockfile as an assertion:
uv sync --locked uv run pytest uv run ruff check .
--locked tells uv not to update the lockfile. If the project metadata no longer matches it, the command fails instead of quietly producing a new resolution. That failure is useful. It forces the dependency change into a reviewable commit.
Use a regular uv lock only when you intentionally want to resolve changes. For example, when adding a dependency, upgrading a package, or refreshing the graph after reviewing a security advisory. Dependency updates should be a deliberate operation, not incidental background activity caused by a new machine.
A minimal CI job should check out the repository, install the pinned Python version, install uv, run uv sync --locked, and execute tests and static checks through uv run. The important property is not the CI vendor. It is that the job does not inherit arbitrary global packages or resolve a new graph during a test run.
Isolation prevents accidental inputs
Global Python packages are an invisible dependency source. So are activated environments left over from another project, PYTHONPATH overrides, editable installs outside the repository, and user-level pip configuration.
Use one project environment per repository. Run tools through uv run so the command is attached to the project environment rather than the shell state. When debugging a discrepancy, print the basics early: Python version, platform, uv version, and the installed package set. A small diagnostic step can save an hour of comparing stack traces.
Configuration needs the same discipline. Commit a sanitized example such as .env.example, but do not treat a developer’s local .env file as a reproducible input. Secrets should come from the environment or a CI secret store. Non-secret settings that affect behavior - feature flags, region, timezone, locale, and database dialect - should be explicit in test commands or documented test fixtures.
Platform differences are real constraints
A lockfile can represent packages for more than one platform, but it cannot erase platform differences. A package may select a different wheel on Apple Silicon than on Linux x86_64. Another may compile against OpenSSL, PostgreSQL, or a C compiler installed by the operating system.
The practical answer depends on where the code runs. If you ship a macOS developer tool, test on the supported macOS versions and architectures. If production runs in Linux containers, test and build inside the same base-image family. If you support both, keep separate CI coverage instead of assuming a successful macOS install proves Linux compatibility.
Containers help when the operating system and native build chain are part of the problem. They are not automatically required for every Python project, and they add their own image maintenance work. A focused CLI with pure-Python dependencies may only need a pinned interpreter and uv.lock. A service with native database drivers likely needs a pinned base image as well.
Treat dependency updates as engineering work
Reproducibility does not mean freezing dependencies forever. It means knowing exactly what changed when you update them. Schedule updates, inspect the lockfile diff, run tests, and record the reason for larger upgrades. A narrow update is easier to diagnose and revert than six months of accumulated changes.
Security scanning belongs in this workflow, but it answers a different question. A lockfile tells you what will install. A vulnerability scanner tells you whether that exact graph includes known issues. Scan the lockfile, not a guessed environment and not only the direct dependencies. Wizhut's Practical UV is built around this distinction: findings are uv.lock-accurate and focused on the dependencies you directly chose, so a warning has a clear path back to a project decision.
Do not blindly upgrade every flagged package. A transitive fix may require upgrading the direct package that introduced it. A patched version may alter an API or drop an older Python version. The right sequence is to identify the locked version, trace why it is present, select a compatible remediation, then regenerate and test the lockfile.
Make drift fail early
The strongest reproducibility rule is simple: if an input changes, the build should either use the recorded new input or fail. Do not let it silently choose a third option.
Commit the interpreter pin and lockfile. Create isolated environments. Install with uv sync --locked in CI. Test on the platforms you claim to support. Keep machine-specific configuration outside the repository, and make behavior-changing settings explicit. Then an environment failure becomes a concrete diff or a direct setup error, not a rumor that “it worked on my machine.”
That is the useful outcome: less time rebuilding someone else’s laptop state, and more time changing the code you meant to change.