Python Dependency Locking with PEP 751 and pylock.toml

Unpack PEP 751 and the new pylock.toml standard, designed to bring tool-agnostic, secure, and reproducible dependency resolution to the Python ecosystem.

MiHiR SEN
MiHiR SEN
·3 min read
This article explores PEP 751 and the pylock.toml format, a tool-agnostic standard for Python lock files. It details how the new format improves on requirements.txt, and demonstrates how to generate and sync lock files across pip, uv, and PDM.

Dependency management in Python is on the verge of a major unification. Historically, developers seeking reproducible builds have faced a highly fragmented landscape. Different packaging tools, such as Poetry, PDM, and Pipenv, have maintained their own custom lock file formats. This lack of standardization meant that a lock file generated by one tool could rarely be understood by another, forcing development teams into strict vendor lock-in.

To bridge this divide, the Python packaging community introduced PEP 751, establishing a standard, tool-agnostic lock file format: pylock.toml. Rather than serving as an internal detail for a specific tool, pylock.toml is designed to be a universal, structured record of a fully resolved dependency graph that any compliant Python installer can consume.

Moving Past requirements.txt and pip-compile

For years, developers have relied on requirements.txt files generated by pip freeze or pip-compile to pin dependencies. However, requirements.txt is not a structured lock file; it is a list of sequential commands intended for the pip installer.

While pip-compile improves security by including hashes, it struggles to manage complex cross-platform environments or multiple distinct dependency groups. In contrast, pylock.toml records not just a list of versions, but the exact resolved state of the environment, storing artifact hashes, download URLs, and OS-specific wheel metadata. This shifts the process from guessing what to download to executing a verified installation of specific files.

Working Across Tools: Pip, UV, and PDM

Starting with recent experimental releases, modern Python packaging tools are actively implementing PEP 751. Developers can now generate, inspect, and install packages using this unified format across different runtimes.

Generating a Lock File with Pip

Using an experimental flag, standard pip can resolve a basic requirements.txt file and export a fully formed pylock.toml lock file. This process triggers pip's resolver, which builds the complete dependency graph and pins every sub-dependency with its cryptographic hash:

Bash
1# Generate a standard lock file from explicit requirements 2python -m pip lock --requirement requirements.txt

This creates a pylock.toml file containing project metadata, package versions, and artifact references. The resulting lock file ensures that subsequent installations are identical, tamper-proof, and fast.

Multi-Platform Locking and UV

The fast Python packaging tool uv provides robust support for exporting cross-platform lock files. Because uv can resolve environments for multiple operating systems and architectures simultaneously, it generates a comprehensive pylock.toml that is highly portable:

Bash
1# Initialize a project and generate a cross-platform lock 2uv init --bare 3uv add -r requirements.txt 4uv pip sync pylock.toml --preview-features pylock

Because the exported lock file complies with PEP 751, developers are not forced to use uv to install it. A teammate using PDM can consume the exact same lock file to build their local virtual environment, illustrating the power of a tool-agnostic standard:

Bash
1# Syncing the uv-generated lock file using PDM 2pdm use .venv 3pdm sync --lockfile pylock.uv.toml --no-self

The Path to Full Adoption

While PEP 751 offers a powerful foundation for reproducible Python environments, the ecosystem is still in the early stages of adoption. Currently, tools like pip, uv, and PDM support pylock.toml through experimental flags rather than as their default native workflows.

Furthermore, advanced features of the specification, such as multi-environment dependency grouping, are not yet uniformly implemented across all engines. Some tools may ignore complex dependency groups or fail to emit them correctly. Nevertheless, as these implementations mature, PEP 751 and pylock.toml are poised to become the standard for professional Python dependency management, removing tool fragmentation once and for all.