Manage Dependencies - PDM - Python Development Master

PDM provides a bunch of handful commands to help manage your project and dependencies. The following examples are run on Ubuntu 18.04, a few changes must be done if you are using Windows.

Initialize a project#

$ mkdir pdm-test && cd pdm-test
$ pdm init

Answer several questions asked by PDM and a pyproject.toml will be created for you in the project root:

name = "pdm-test"
version = "0.0.0"
description = ""
authors = [
    {name = "Frost Ming", email = ""}
license = {text = "MIT"}
requires-python = ">=3.7"

dependencies = []

If pyproject.toml is already present, it will be updated with the metadata. The metadata format follows the PEP 621 specification

For details of the meaning of each field in pyproject.toml, please refer to Project File.

Add dependencies#

pdm add can be followed by one or several dependencies, and the dependency specification is described in PEP 508.

PDM also allows extra dependency groups by providing -G/--group <name> option, and those dependencies will go to [project.optional-dependencies.<name>] table in the project file, respectively.

After that, dependencies and sub-dependencies will be resolved properly and installed for you, you can view pdm.lock to see the resolved result of all dependencies.

Add local dependencies#

Local packages can be added with their paths:

Local packages can be installed in editable mode (just like pip install -e <local project path> would) using pdm add -e/--editable <local project path>.

Add development only dependencies#

New in 1.5.0

PDM also supports defining groups of dependencies that are useful for development, e.g. some for testing and others for linting. We usually don't want these dependencies appear in the distribution's metadata so using optional-dependencies is probably not a good idea. We can define them as development dependencies:

$ pdm add -dG test pytest

This will result in a pyproject.toml as following:

test = ["pytest"]

For backward-compatibility, if -G/--group is not given, dependencies will go to dev group under [] by default.


The same group name MUST NOT appear in both [] and [project.optional-dependencies] .

Save version specifiers#

If the package is given without a version specifier like pdm add requests. PDM provides three different behaviors of what version specifier is saved for the dependency, which is given by --save-<strategy>(Assume 2.21.0 is the latest version that can be found for the dependency):

Update existing dependencies#

To update all dependencies in the lock file:

To update the specified package(s):

To update multiple groups of dependencies:

$ pdm update -G security -G http

To update a given package in the specified group:

$ pdm update -G security cryptography

If the group is not given, PDM will search for the requirement in the default dependencies set and raises an error if none is found.

To update packages in development dependencies:

# Update all default + dev-dependencies
$ pdm update -d
# Update a package in the specified group of dev-dependencies
$ pdm update -dG test pytest

About update strategy#

Similarly, PDM also provides 2 different behaviors of updating dependencies and sub-dependencies, which is given by --update-<strategy> option:

Remove existing dependencies#

To remove existing dependencies from project file and the library directory:

# Remove requests from the default dependencies
$ pdm remove requests
# Remove h11 from the 'web' group of optional-dependencies
$ pdm remove -G web h11
# Remove pytest-cov from the `test` group of dev-dependencies
$ pdm remove -dG test pytest-cov

Install the packages pinned in lock file#

There are two similar commands to do this job with a slight difference:

Select a subset of dependencies with CLI options#

Say we have a project with following dependencies:

[project]  # This is production dependencies
dependencies = ["requests"]

[project.optional-dependencies]  # This is optional dependencies
extra1 = ["flask"]
extra2 = ["django"]

[]  # This is dev dependencies
dev1 = ["pytest"]
dev2 = ["mkdocs"]
Command What it does Comments
pdm install install prod and dev deps (no optional)
pdm install -G extra1 install prod deps, dev deps, and "extra1" optional group
pdm install -G dev1 install prod deps and only "dev1" dev group
pdm install -G:all install prod deps, dev deps and "extra1", "extra2" optional groups
pdm install -G extra1 -G dev1 install prod deps, "extra1" optional group and only "dev1" dev group
pdm install --prod install prod only
pdm install --prod -G extra1 install prod deps and "extra1" optional
pdm install --prod -G dev1 Fail, --prod can't be given with dev dependencies Leave the --prod option

All development dependencies are included as long as --prod is not passed and -G doesn't specify any dev groups.

Besides, if you don't want the root project to be installed, add --no-self option, and --no-editable can be used when you want all packages to be installed in non-editable versions. With --no-editable turn on, you can safely archive the whole __pypackages__ and copy it to the target environment for deployment.

Show what packages are installed#

Similar to pip list, you can list all packages installed in the packages directory:

Or show a dependency graph by:

$ pdm list --graph
tempenv 0.0.0
└── click 7.0 [ required: <7.0.0,>=6.7 ]
black 19.10b0
├── appdirs 1.4.3 [ required: Any ]
├── attrs 19.3.0 [ required: >=18.1.0 ]
├── click 7.0 [ required: >=6.5 ]
├── pathspec 0.7.0 [ required: <1,>=0.6 ]
├── regex 2020.2.20 [ required: Any ]
├── toml 0.10.0 [ required: >=0.9.4 ]
└── typed-ast 1.4.1 [ required: >=1.4.0 ]
bump2version 1.0.0

Set PyPI index URL#

You can specify a PyPI mirror URL by following commands:

$ pdm config pypi.url

By default, PDM will read the pip's configuration files to decide the PyPI URL, and fallback to if none is found.

Allow prerelease versions to be installed#

Include the following setting in pyproject.toml to enable:

allow_prereleases = true

Environment variables expansion#

For convenience, PDM supports environment variables expansion in the dependency specification under some circumstances:

Don't worry about credential leakage, the environment variables will be expanded when needed and kept untouched in the lock file.

Save disk space by enabling the install cache#

When using virtualenv to isolate project dependencies, if you have 100 projects depending on the same package, you will end up with 100 copies of that dependency. With PDM, you can opt in the installation caching so that the dependency will be installed into a centrialized store and be used by multiple projects. To enable it, simply do:

$ pdm config feature.install_cache on

Add --local option to enable for the current project only.

This feature will only cache the normal wheel installations, i.e. installing from source won't be cached.

当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
若有不妥, 欢迎评注提醒:


订阅 substack 体验古早写作:

点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:


关于 ~ DebugUself with DAMA ;-)
公安备案号: 44049002000656 ...::