Meet PDM: The Modern Python Package Manager for Easier Dependency Control
Managing project dependencies in Python can get complicated as your projects grow. Developers often rely on virtual environments to keep things separate, but newer tools like PDM aim to simplify this process. PDM is a package manager that combines many features for managing packages, virtual environments, and project setup, all built on Python’s standards.
Getting Started with PDM
To use PDM, you need Python 3.7 or newer. The best way to install it is into your user directory, not directly into the Python installation. You can do this easily with a command like pip install --user pdm. Once installed, typing pdm in your command line should bring up the tool. If you have multiple Python versions on Windows, you can run py - to make sure you’re using the right one. You can also update PDM itself with pdm self update.
Managing Dependencies with PDM
Creating a new project is simple. Just go to the folder where you want your project and run pdm new <project-name>. To add dependencies, use pdm add <dependency>. You can add multiple dependencies at once and should follow the standard format from PEP 508. Removing dependencies is just as easy with pdm remove <dependency>>. Remember, each time you add or remove something, PDM recalculates the dependency graph, which might take some time. To see which packages you have, run pdm list, or use pdm list --graph to view dependencies in a tree format.
When it’s time to install dependencies, pdm install is your go-to. It creates a lock file that records specific package versions and installs everything accordingly. If you want to install only what's listed in an existing lock file, pdm sync does the job faster, skipping dependency resolution.
Handling Optional and Development Dependencies
By default, dependencies are installed without specific labels. If you need to specify whether a dependency is for development or production, you can group them. Using pdm add -dG dev black adds the black formatter to a development group. To mark a package as needed only in production, use pdm add -d <dependency>. For example, if your app uses the regex module only in production, you'd add it with pdm add -d regex.
Updating dependencies is straightforward: pdm update updates all packages, or update individual ones with pdm update <package>. To update only pinned dependencies to their latest versions, add the --update-eager flag. If you want to clean up unused packages, run pdm sync --clean, but only inside your project directory, not on your system Python.
Running Python Code with PDM
You can execute scripts or commands inside your project with the pdm run command. For example, pdm run black will format your code, or pdm run myscript.py runs a specific script. PDM can also run single-file scripts, and if your script includes special metadata at the top, PDM will create a temporary environment with the needed dependencies before running it.
Using __pypackages__ with PDM
Although PDM was designed with a feature called PEP 582 in mind—allowing packages to be stored in a __pypackages__ directory—this idea was rejected by the Python community. Still, PDM supports this setup for individual projects or globally, which can be handy if you want to include custom or modified packages directly in your source folder. That said, PDM generally recommends sticking with virtual environments because they are more widely supported and provide better isolation.
All in all, PDM offers a modern approach to managing Python projects, reducing the reliance on virtual environments while offering powerful dependency management features. Whether you're building new projects or maintaining existing ones, it’s worth exploring how PDM can streamline your workflow.















What do you think?
It is nice to know your opinion. Leave a comment.