PDM is a new Package manager for Python. You may not even know it exists, but PDM has been around for two years with a 1.0 release in 2021. The highest version is 1.12.8.

When I first heard about PDM, I automatically assumed it was Python Development Manager, another virtual environment management tool in the same vein as Pipenv and Poetry.

It wasn’t until I read the author’s blog that I learned that PDM stands for Python Development Master, which is even better than I expected.

It is worth mentioning that the author of PDM is a member of PyPa, one of the main maintainers of Pipenv, and most importantly, he is Chinese, so it is a tool developed by Chinese people.

# 1. Why PDM?

Early package managers (e.g. Pipevn, Poetry) were based on virtual environments, which were primarily designed to isolate project development environments, but when it came to nesting virtual environments into virtual environments, the problem was tricky and often problematic.

PDM benefited from a 2018 PEP proposal (PEP582, Python Local Packages Directory) that did away with virtual environments entirely.

According to the author’s blog, the reason for the original wheel was because Poetry and Pipenv weren’t good enough, and there’s PEP582 out there to develop a revolutionary Python package management tool called PDM.

PDM contains the following features:

  • PEP 582 local project library directory that supports installing and running commands without the need for a virtual environment.

  • A simple and relatively fast dependency resolver, especially for large binary package distributions.

  • PEP 517-compliant build back end for building distribution packages (source and Wheel format)

  • Have a flexible and powerful plug-in system (plug-in system directly opened a level)

  • PEP 621 metadata format

  • Centralized installation cache like PNPM saves disk space

Although PDM is developed by Chinese, the official website documents are in English for internationalization.

I spent a whole day reading through the document and digested 70% of the usage of PDM. Here are some tips to help you get started with PDM.

There is a lot about PDM, and I plan to introduce it completely in two parts:

  • An entry-level tutorial for beginners

  • A tutorial for professional players

This is the first article to give you a framework for understanding the basic usage of PDM. Please keep an eye on the following articles for the real competitiveness of PDM.

# 2. Install PDM

There are many ways to install PDM, there are 6 kinds on the official website, such as PIP, PIpx, Homebrew and so on

In previous articles, I recommended the PIpx tool as a great tool for installing packages for command-line applications.

Details: Use the PIpx tool to install the command line tool

At this point, PDM is a command line tool, so I also recommend using PIpx installation, convenient for unified command line management

To install it, run pipx install PDM

PDM can only be used with Python 3.7+. To install PDM using other methods, you need to ensure that your Version of Python is available first, but you don’t need to worry about using PIpx.

# 3. Initialize PDM

Executing PDM init will start the initialization, and at the time of initialization, it will let you select some information about the project

  • Whether to upload PyPI

  • The Python version of the dependency

  • License type

  • The author information

  • Your email information

I have Python 2.7 and Python 3.10 on my machine. When initializing the project, I will scan all Python versions on my machine and ask you to select the Python version of the project.

When you’re done, PDM writes your selection in tomL format to the PyProject.toml configuration file.

# 4. PDM usage

PDM has a lot of commands, use -h to see the help menu

4.1 the installation package

Just like Poetry, the add command is installed, but PDM’s Add is easier to use than Poetry

4.2 check the package

Use PDM List to list the installed packages for the current environment

Add a –graph to view it as a tree, and you can see the hierarchy of direct and indirect dependencies

PDM list also has two options:

  • — Freeze: Lists installed packages in requirements.txt format

  • –json: Lists installed packages in JSON format, but must be used in conjunction with –graph

To view the details of a package body, just use PDM show

4.3 remove the packages

The package is removed using the remove command

4.4 Project Configuration

Print the environment configuration for the project without any parameters

You only need to add key and value as parameters. The pYPI mirror agent is used as an example

Now I’m going to change it to Ngari source, just execute the following command, much easier than poetry

PDM config there are a lot of configuration, want to clear up one by one can go to the website to consult: PDM. Fming. Dev/configurati…

4.5 Running Commands

To execute a command or project in a PDM environment, you can use the run command. If there are many parameters when executing a project, you can configure the command alias in PyProject. toml

4.6 Viewing the Environment

Using the info command, you can view the environment information of the current project

4.7 update

For updates, use the following two for simple scenarios

PDM update < PKG >Copy the code

Complex scenarios, PDM also take you into account, it provides a lot of options, you can use according to the need.

  • –save-compatible: The project depends on compatible versions

  • –save-wildcard: Save the wildcard version

  • –save-exact: saves the package of the specified version

  • –save-minimum: Keep the minimum version of the package

  • –update-reuse: tries to update only the package specified on the command line, or not if the dependent package is not updated at all

  • –update-eager: updates a package with dependencies (recursive upgrades)

  • Prerelease: prerelease allows for early release

  • — Unconstrained: The package can be upgraded to the latest version regardless of the package version constraint

  • –top: Update only packages that are present in PyProject.toml

  • Dry-run: test run without modifying the lock file

  • –no-sync: update lock files, but not packages

If your dependencies have groupings, you can also specify groupings to update

pdm update -G security -G http

Copy the code

You can also specify a group to update a package within the group

pdm update -G security cryptography

Copy the code

Add -d to specify dev dependencies again

PDM update -dg test pytestCopy the code

Similarly, you can specify –prod or –production to upgrade packages that are not dev.

4.8 switch py

When you initialize the PDM project, you have selected the current Python version and the range of Python versions available. If you want to change it later, you can use the use command, but the version is subject to the previously specified range.

Given that the allowed range is Python 3.9+ and you are currently using Python 3.10, you can simply switch over.

Product data management (PDM) use python3.9Copy the code

# 5. Command alias

Add [tool.pdm.scripts] to PyProject.toml to alias shortcut commands, which can be useful if the project execution has a lot of parameters.

[tool.pdm.scripts] has two forms

[tool.pdm.scripts] start = {CMD = "python main.py"}Copy the code

But if you want to comment parameters, you have to use a second method, such as this

[tool.pdm.scripts] start = {cmd = [ "flask", "run", # Important comment here about always using port 54321 "-p", "54321"]}Copy the code

In addition to CMD, there are two parameters

Subprocess.popen () with shell=True

One is the env_file parameter, which specifies the file to configure environment variables

[tool.pdm.scripts]
start.cmd = "flask run -p 54321"
start.env_file = ".env"

Copy the code

If you want to make the environment variable file not limited to a single command, but PDM run globally, you can do this

[tool.pdm.scripts]
_.env_file = ".env"

Copy the code

Add –list or -l to see quick aliases for all Settings

Pre and post commands can be set for each shortcut command:

  • Pre command: the pre command is executed before each shortcut command

  • The post command is executed after each shortcut command is executed

[tool.pdm.scripts] pre_compress = "{{ Run BEFORE the `compress` script }}" compress = "tar czvf compressed.tar.gz data/"  post_compress = "{{ Run AFTER the `compress` script }}"Copy the code

# 6. Automatic completion

Although PDM commands are many, they are not complicated, so it is not necessary to use auto-completion. If you really need auto-completion, you can also achieve it.

For different shells, the configuration mode of auto completion is different, which is explained in detail on the official website.

If you use ZSH like I do, follow my instructions.

Vim ~/.zshrc is used to configure the PDM plug-in into ZSH

plugins=(git z macos extract zsh-syntax-highlighting zsh-autosuggestions pdm)

Copy the code

# 7. Compatibility of schemes

Other solutions are migrated to PDM

PDM is easy enough and open enough that if you are currently using another package manager like Pipenv, Poetry, or are still using the original requirements.txt, you can easily migrate to PDM:

  • Use PDM import -f {file} without initialization, direct conversion

  • When executing PDM Init or PDM install, it will automatically recognize your current dependencies and convert them

PDM migration to another solution

Similarly, you can also export PDM managed projects for other solutions

Pyproject. toml and pdm.lock are the two core files of PDM.

As a rising star, PDM does not forget its roots. It supports:

  • Convert PyProject.toml to setup.py

    pdm export -f setuppy -o setup.py
    
    Copy the code
  • Convert pdm.lock to requirements.txt

    pdm export -o requirements.txt
    
    Copy the code

# 8. To sum up

With a lot of effort, the basic usage of PDM has been introduced, and I’m sure some people will question: is this what you call a revolutionary package manager?

As a matter of fact, the above is just a beginning operation, and some of the core knowledge of PDM. Considering the limited space, I will arrange the content of these advanced classes in the following articles, which will include but not limited to:

  • The rationale for PDM: PEP 582 proposal

  • Build the release package: THE PEP 517 proposal

  • Definition and use of Hook script

  • Plug-in management system and custom plug-ins

  • Introduction to cache management system

These things are at the heart of PDM, and only when you understand them can you really use PDM well. At that point, you’ll wonder why Guido doesn’t include this tool as a standard package management tool.

For PDM further advanced articles, please pay attention to my personal public number “Write some code Mingo”

I’ve written articles about packages and virtual environment management tools, many of them

Those interested in learning more systematically can visit python.iswbm.com/