SoFunction
Updated on 2024-11-18

Python package management tools of PDM tutorials

preamble

Hello, I'm Ginger. Previously talked about a pipenv and poetry package management tool, I think you must be like me to understand the pipenv has problems, so it is not very recommended to use. Later changed to the poetry tool, but this thing for me is a little bit of trouble, often easy to hit the wrong, although it can be made into the form of alias. So today I recommend a tool called PDM, full name: Python Development Master. it is also very good, interested friends can use it.

Installation and use of PDM

# macos install pipx
brew install pipx
pipx ensurepath

# Linux install pipx
python3 -m pip install pipx
python3 -m pipx ensurepath

Install pdm with pipx

PDM is only available for Python 3.7+. To install it using any other method, make sure you have the Python version first, but with pipx you don't need to worry about it. Be sure to pay attention to the version of pipx you are installing and the version of Python pipx uses to install pdm, otherwise pdm init will fail!

pipx install pdm
pipx list

Configuring pdm auto-completion

# zsh method
mkdir $ZSH_CUSTOM/plugins/pdm
pdm completion zsh > $ZSH_CUSTOM/plugins/pdm/_pdm

vim ~/.zshrc
plugins=(
         pdm
         poetry
         git
         zsh-completions
         zsh-autosuggestions
)

source ~/.zshrc

pdm info

PDM Initialization Project

Executing pdm init will start the initialization, which will let you select some information about the project:.

  • Whether to upload PyPI
  • Dependent Python Versions
  • License Type
  • Author Information
  • Mailbox information All Python versions on the machine are scanned out when the project is initialized, and it will let you select the Python version of the project.
❯ mkdir pdm-demo
❯ cd pdm-demo
# Re-execute pdm successfully
❯ pdm init
Creating a  for PDM...
Please enter the Python interpreter to use
0. /Users/allenjol/.pyenv/versions/3.10.4/bin/python3 (3.10)
1. /Users/allenjol/.pyenv/versions/3.10.4/bin/python (3.10)
2. /Users/allenjol/.pyenv/versions/3.10.4/bin/python3.10 (3.10)
3. /Users/allenjol/.pyenv/versions/3.10.4/bin/python3.10 (3.10)
4. /Users/allenjol/.pyenv/versions/3.10.4/bin/python3 (3.10)
5. /Users/allenjol/.pyenv/versions/3.10.4/bin/python (3.10)
6. /Library/Frameworks//Versions/Current/bin/python3.10 (3.10)
7. /Library/Developer/CommandLineTools/usr/bin/python3 (3.8)
8. /Users/allenjol/.local/pipx/venvs/pdm/bin/python (3.10)
Please select: [0]: 8
Using Python interpreter: /Users/allenjol/.local/pipx/venvs/pdm/bin/python (3.10)
Is the project a library that will be uploaded to PyPI? [y/N]: N
License(SPDX name) [MIT]:
Author name [allen_jol]:
Author email [471733951@]:
Python requires('*' to allow any) [>=3.10]:
Changes are written to .

# When you are done, PDM writes your selections to the configuration file in toml format.
❯ ll
total 8
-rw-r--r--  1 allenjol  staff  283 May 20 13:58 


❯ cat 
[project]
name = ""
version = ""
description = ""
authors = [
    {name = "allen_jol", email = "471733951@"},
]
dependencies = []
requires-python = ">=3.10"
license = {text = "MIT"}

[]

[build-system]
requires = ["pdm-pep517>=0.12.0"]
build-backend = "pdm."

PDM Common Commands

# Installation packages
pdm add requests

# View Package
pdm list

# Viewed in tree form
pdm list --graph

# View package details
pdm show requests

# Delete packages
pdm remove requests

# Project configuration (without any parameters, you can print out the environment configuration of the project)
pdm config

# Modify pypi source
pdm config  /pypi/simple
pdm config 

Run command

To execute a command or project in the pdm environment, you can use the run command. If you are executing a project with many parameters, you can configure the command alias in the Configuring command aliases

$ cat 
print('Hello, pdm')

$ pdm run python 
hello, pdm

View Environment

pdm info -v
pdm info --env
pdm info --packages
pdm info --where
pdm info --python

update packages

# Update all packages
pdm update 

# Update a package
pdm update <pkg>

For complex scenarios, pdm has taken care of that for you as well. It provides many options that can be used as needed.

  • --save-compatible: The project depends on compatible versions.
  • --save-wildcard: save wildcard version (not understood yet)
  • --save-exact: Saves the package with the specified exact version.
  • --save-minimum: keep the minimum version of the package
  • --update-reuse: Try to update only the packages specified in the command line, and not update their dependencies if you can
  • --update-eager: update a package with its dependencies (recursive upgrade)
  • --prerelease: allow early release (not understood yet)
  • --unconstrained: Ignore package version constraints and upgrade the package to the latest version.
  • --top: only update packages that are in the
  • --dry-run: try running without modifying the lock file.
  • --no-sync: update only the lock file, but not the packages

If your dependency packages have groups set up, you can also specify the groups to update

pdm update -G security -G http

You can also specify a group to update a package in the group.

pdm update -G security cryptography

Add -d to specify the dev dependency again.

# Update all dev dependencies
pdm update -d

# Update a package in a group under a dev dependency
pdm update -dG test pytest

You can also specify --prod or --production to upgrade non-dev (i.e. production) packages.

Switching Python Versions

When you initialize your pdm project, you have already selected the current Python version and the range of available Python versions. If you want to change it later, you can use the use command, but the version will be bound by the previously set version range. If you want to change the version later, you can use the use command, but the version will be constrained by the previously set version range. Suppose the allowed range is python 3.9+, and you're currently using python 3.10, so you can switch over.

pdm use python3.9

command alias

In Add [] you can set a shortcut command alias, which will be useful if the project is executed with a very large number of parameters.

$ tail -n 2 
[]
start = 'python '

$ pdm run start
Running cmd script: ['python', '']
Hello, pdm

[] In two forms

# The first
[]
start = "python "

# The second
[]
start = {cmd = "python "}

If you want to add a comment to a parameter, you have to use the second method, for example, like this

[]
start = {cmd = [
    "flask",
    "run",
    # Important comment here about always using port 54321
    "-p", "54321"
]}

In addition to cmd, there are two other parameters, one is the shell parameter, from the output you should be able to tell the difference between cmd and () with shell=True, which is pretty much the same thing.

$ tail -n 2 
[]
start = {shell = "python "}

$ pdm run start
Running cmd script: python 
Hello, pdm

One is the env_file parameter, which allows you to specify the file in which environment variables are configured.

[]
 = "flask run -p 54321"
start.env_file = ".env"

If you want to limit the file for this environment variable not only to a particular command, but to pdm run globally, you can configure it this way

[]
_.env_file = ".env"

Program compatibility

Other programs migrating to pdm

pdm is good enough and open enough that if you are currently using another package manager, such as pipenv, poetry, or still using the original, you can easily migrate to pdm:

Use pdm import -f to convert without initialization

When you execute pdm init or pdm install, it will automatically recognize your current dependencies and convert pdm to migrate to other programs It can also be used as a pdm-managed project to export to other programs and are the two core files of pdm. pdm has not forgotten its roots as an upstart, and it supports:

will be converted to

pdm export -f setuppy -o 

will be converted to

pdm export -o 

pycharm configuration support for PDM

Create a project directory on the command line, go into the project directory and execute init to initialize it, set up the pypi source a bit, and then use pycharm to open this directory.

mkdir pdm-demo
cd pdm-demo

pdm config 
pdm config  /pypi/simple –-trusted-host 
pdm config 

# Permanent setup under Linux
❯ vi ~/.pip/
[global]
index-url=/pypi/simple/
[install]
trusted-host=


# Install a package using pdm:
pdm add -v requests flask

After completing the installation, we can see the __pypackages_ directory in the current directory, the libraries we installed earlier are isolated and installed under it, similar to the node_modules directory of node. In this way, we can realize the effect of project-level environment isolation, and do not need to create additional virtual environments. To use the local isolated libraries under the pdm project to execute programs in the IDE is very simple, take pycharm for example, open the pdm project as a pycharm project, and then locate the lib folder in the location shown in the picture, and then mark it as the Sources Root, and at the same time remember to Also remember to select the interpreter corresponding to the environment in which pdm init was initialized.

Open the pdm-demo project with pycharm, find the lib folder in the location shown in the picture, mark it as Sources Root, and remember to select the Python interpreter corresponding to the environment where pdm init was initialized.

pycharm sets the Python interpreter to the one that corresponds to the environment in which pdm init was initialized: first check the pdm info, you can see the Python Interpreter interpreter path

❯ pdm info
PDM version:        1.15.0
Python Interpreter: /Users/allenjol/.local/pipx/venvs/pdm/bin/python (3.10)
Project Root:       /Users/allenjol/Documents/Codes/pdm-demo
Project Packages:   /Users/allenjol/Documents/Codes/pdm-demo/__pypackages__/3.10

Just set the Python Interpreter path in pycharm to be the same as the one in the pdm info.

to this article on the use of Python package management tools PDM tutorial article is introduced to this, more related Python package management tools PDM content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!