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
<code># macOS install pipx
brew install pipx
pipx ensurepath
# Linux install pipx
python3 -m pip install pipx
python3 -m pipx ensurepath</code>Install PDM with pipx
PDM requires Python 3.7+. Using pipx avoids worrying about the Python version used for installation.
<code>pipx install pdm
pipx list</code>Configure PDM auto-completion
<code># 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</code>Initialize a PDM project
Running
pdm initprompts for project information such as PyPI upload, Python version, license, author, and email.
Upload to PyPI?
Python version
License type
Author information
<code>mkdir pdm-demo
cd pdm-demo
pdm init</code>Common PDM Commands
<code># 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</code>Run commands
Use
pdm runto execute commands within the PDM environment.
<code>cat main.py
print('Hello, pdm')
pdm run python main.py</code>View environment
<code>pdm info -v
pdm info --env
pdm info --packages
pdm info --where
pdm info --python</code>Update packages
<code># Update all packages
pdm update
# Update a specific package
pdm update <pkg></code>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.9to change the interpreter within the constraints set during initialization.
Command aliases
Add scripts under
[tool.pdm.scripts]in
pyproject.tomlto create shortcuts.
<code># Example
[tool.pdm.scripts]
start = "python main.py"
# or
start = {cmd = "python main.py"}</code>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 -for automatically during
pdm init. It can also export to
setup.pyor
requirements.txt.
<code>pdm export -f setuppy -o setup.py
pdm export -o requirements.txt</code>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.10lib folder as Sources Root and select the same Python interpreter shown by
pdm info.
<code>pdm info
# shows interpreter path and project packages location</code>The article concludes that PDM is powerful, flexible, and worth trying for Python development.
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.