PDM Build Locked
pdm-build-locked is a PDM plugin that adds locked packages as additional optional dependency groups to the distribution metadata. This enables reproducible installs of Python CLI tools by allowing users to install exact dependency versions from a PDM lockfile, preventing breakage on dependency updates. It is compatible with PDM versions >=2.11 and is currently at version 0.3.7.
Common errors
-
ResolutionImpossible: A locked group could not be resolved during installation (e.g., `pip install mypkg[locked]` fails).
cause This often occurs when `tool.pdm.resolution.overrides` in your `pyproject.toml` sets a version range for a dependency that conflicts with the range specified in `project.dependencies` for the same package.fixReview your `pyproject.toml` and reconcile any conflicting version constraints between `project.dependencies` and `tool.pdm.resolution.overrides`. The override should refine, not contradict, the base dependency. -
ERROR: Requested groups not in lockfile: ['locked'] or other custom optional groups.
cause The `pdm.lock` file that was used to build the package does not contain the specified dependency group, or it's outdated/corrupted.fixRemove the existing `pdm.lock` file and regenerate it using `pdm lock -G :all` (or specifying relevant groups) to ensure all intended dependency groups are included and properly locked. Then, rebuild your distribution. -
When installing my built package with `[locked]`, it doesn't use the pinned versions from `pdm.lock`.
cause The `pdm-build-locked` plugin was likely not active or correctly configured during the `pdm build` process, or the `pyproject.toml` wasn't properly set up to use it.fixEnsure `pdm-build-locked` is installed and active (`pdm self add pdm-build-locked` or `pdm add --dev pdm-build-locked` and `pdm install`). Verify that your `pyproject.toml` includes the `[tool.pdm-build-locked]` section. After confirming the setup, rebuild your package using `pdm build --locked`.
Warnings
- breaking Setting `tool.pdm.resolution.overrides` to a version range incompatible with `project.dependencies` for a dependency can lead to `ResolutionImpossible` errors when users try to install the package with the `[locked]` extra.
- gotcha Using `[locked]` dependency groups is primarily intended for CLI tools or CLI tools that are also libraries (where `[locked]` is used only when installing the executable). Applying `[locked]` dependencies for a 'library only' package is highly discouraged as it can easily lead to dependency conflicts for consuming projects.
- gotcha The `pdm.lock` file must be configured with the `inherit_metadata` strategy (for PDM >= 2.11) and include locks for all desired optional-dependencies groups. If groups are missing, they won't be available in the built distribution.
Install
-
pip install pdm-build-locked -
pdm self add pdm-build-locked
Imports
- No direct Python imports for end-users
This library is a PDM plugin and is primarily used via PDM CLI commands and `pyproject.toml` configuration. It does not expose Python symbols for direct import into application code.
Quickstart
# 1. Create a new PDM project and add a dependency mkdir my_cli_app cd my_cli_app pdm init --backend pdm-backend --static-version --no-interaction pdm add rich # 2. Add pdm-build-locked to your project (if not installed globally) pdm add --dev pdm-build-locked # 3. Configure pyproject.toml to use pdm-build-locked # Add the following to your pyproject.toml manually or via script: # [tool.pdm-build-locked] # backend = 'pdm-backend' # # 4. Ensure the plugin is active (if not already via `pdm self add`) pdm install # 5. Build the project with locked dependencies pdm build --locked # 6. Install the resulting wheel with locked dependencies # (Replace 'my_cli_app-0.1.0-py3-none-any.whl' with your actual wheel file name) pip install dist/my_cli_app-0.1.0-py3-none-any.whl[locked] # To verify, you can check installed packages or run your CLI app