uv.lock
Overview
The uv.lock file is an automatically generated lock file used by the Python package manager PDM or similar tools that manage Python dependencies. It captures the exact versions and sources of all dependencies required for a Python project, ensuring reproducible builds and consistent environments across different machines and deployments.
This lock file is crucial for:
Ensuring deterministic installation of dependencies.
Capturing dependency resolution markers such as supported Python versions and platform-specific constraints.
Recording package metadata including version numbers, source registries, download URLs, and hashes for integrity verification.
Managing nested dependencies and their requirements precisely.
Structure and Contents
The uv.lock file is formatted as TOML and consists of several key parts:
1. Global Metadata
version: The format version of the lock file.revision: Revision number for the lock file.requires-python: The Python version constraints for the entire environment.resolution-markers: A list of conditional expressions that describe when certain resolutions apply, such as platform or Python version specific markers.
2. Package Entries
Each package required by the project is listed under a table array [[package]], each including:
name: The package name.version: The resolved version of the package.source: The source registry or repository for the package.dependencies: (Optional) List of other packages this package depends on.sdist: Source distribution URL and its SHA256 hash.wheels: Available wheel distributions with URL and SHA256 hash.resolution-markers: (Optional) Conditions under which the package resolution applies.optional-dependencies: (Optional) Groups of optional dependencies by feature.
Key Implementation Details
Dependency Resolution: The lock file records dependency resolution markers, e.g., platform-specific or Python-version-specific constraints, to ensure that the correct package versions are installed for the current environment.
Integrity Verification: Each package includes SHA256 hashes for both source distributions and wheels to verify the integrity of the downloaded packages.
Mirrors Usage: The package sources are mostly from mirrors (e.g., Alibaba Cloud's PyPI mirror), improving download speed and reliability in certain regions.
Complex Dependency Graph: The lock file captures nested dependencies, including optional features for some packages, ensuring complete environment reproducibility.
Interaction with Other Parts of the System
The
uv.lockfile complements thepyproject.tomlorpdm.toml(or similar project configuration files) that define high-level dependency requirements.During installation (
pdm installor equivalent), the lock file is used to install exact versions of packages recorded here.It enables CI/CD pipelines, development, and production environments to use consistent and verified dependency versions.
Tools that read this lock file can perform dependency audits, vulnerability scanning, or caching based on exact package versions and sources.
Usage Example
To use the uv.lock file in a PDM-managed project:
# Install dependencies exactly as specified in uv.lock
pdm install
# Update dependencies and regenerate uv.lock
pdm update
The lock file should be committed to version control to ensure all collaborators and deployment environments use the same dependency versions.
Mermaid Flowchart: Dependency Resolution Workflow
Below is a simplified flowchart representing how the uv.lock file is used in dependency resolution and installation:
flowchart TD
A[Project Configuration (pyproject.toml)] --> B[Dependency Resolver]
B --> C[Resolve Dependency Tree]
C --> D[Generate uv.lock with exact versions, hashes, markers]
D --> E[Dependency Installer (pdm install)]
E --> F[Download packages from source URLs]
F --> G[Verify package integrity via SHA256 hashes]
G --> H[Install packages into environment]
H --> I[Reproducible environment setup]
Summary
The uv.lock file is a critical component for Python projects that rely on precise dependency management. It ensures that all packages and their transitive dependencies are locked to specific versions, tailored for the target Python version and platform. This enables consistent and reliable project setups across various environments, reducing "works on my machine" issues and facilitating smooth collaboration and deployment.
Note: The uv.lock file is machine-generated and not intended for manual editing but for version control and automated dependency management workflows.