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.
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 ensurepathInstall PDM with pipx
PDM requires Python 3.7+. Using pipx avoids worrying about the Python version used for installation.
pipx install pdm
pipx listConfigure 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 infoInitialize 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
mkdir pdm-demo
cd pdm-demo
pdm initCommon 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.urlRun commands
Use pdm run to execute commands within the PDM environment.
cat main.py
print('Hello, pdm')
pdm run python main.pyView environment
pdm info -v
pdm info --env
pdm info --packages
pdm info --where
pdm info --pythonUpdate 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.txtConfigure 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 locationThe article concludes that PDM is powerful, flexible, and worth trying for Python development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
