Fundamentals 11 min read

Master Python Project Management with PDM: Installation, Commands, and Tips

This guide introduces PDM (Python Development Master), covering its installation via pipx, project initialization, common commands, environment handling, version switching, script aliases, migration from other tools, and PyCharm integration for streamlined Python development.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Master Python Project Management with PDM: Installation, Commands, and Tips

Introduction

In this article the author introduces PDM (Python Development Master), a modern Python package manager that aims to simplify dependency handling and project setup.

Installing and Using 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 requires Python 3.7+. Using pipx avoids worrying about the Python version used for installation.

pipx install pdm
pipx list

Configure 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

Initialize a PDM project

Running pdm init prompts for project information such as PyPI upload, Python version, license, author, and email.

Upload to PyPI?

Python version

License type

Author information

Email

mkdir pdm-demo
cd pdm-demo
pdm init

Common PDM Commands

# Install package
pdm add requests

# List packages
pdm list

# List as graph
pdm list --graph

# Show package details
pdm show requests

# Remove package
pdm remove requests

# Show project config
pdm config

# Change PyPI source
pdm config pypi.url http://mirrors.aliyun.com/pypi/simple
pdm config pypi.url

Run commands

Use pdm run to execute commands within the PDM environment.

cat main.py
print('Hello, pdm')

pdm run python main.py

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 specific package
pdm update <pkg>

Additional options include --save-compatible, --save-wildcard, --save-exact, --save-minimum, --update-reuse, --update-eager, --prerelease, --unconstrained, --top, --dry-run, and --no-sync.

Switch Python version

Use pdm use python3.9 to change the interpreter within the constraints set during initialization.

Command aliases

Add scripts under [tool.pdm.scripts] in pyproject.toml to create shortcuts.

# Example
[tool.pdm.scripts]
start = "python main.py"
# or
start = {cmd = "python main.py"}

Aliases can include comments, shell execution, and environment files.

Compatibility with Other Tools

PDM can import projects from pipenv, poetry, or a requirements.txt file using pdm import -f or automatically during pdm init. It can also export to setup.py or requirements.txt.

pdm export -f setuppy -o setup.py
pdm export -o requirements.txt

Configure PDM in PyCharm

Create a project directory, run pdm init, set the PyPI source, and open the folder in PyCharm. Mark the __pypackages__/3.10 lib folder as Sources Root and select the same Python interpreter shown by pdm info.

pdm info
# shows interpreter path and project packages location

The article concludes that PDM is powerful, flexible, and worth trying for Python development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PyCharmproject setupCLI toolsenvironment isolationpackage-managementPDM
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.