PDM - Python Development Master
source link: https://pdm.fming.dev/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Introduction#
PDM is a modern Python package manager with PEP 582 support. It installs and manages packages
in a similar way to npm
that doesn't need to create a virtualenv at all!
Feature highlights#
- PEP 582 local package installer and runner, no virtualenv involved at all.
- Simple and relatively fast dependency resolver, mainly for large binary distributions.
- A PEP 517 build backend.
- PEP 621 project metadata.
Installation#
PDM requires Python 3.7+ to be installed. It works on multiple platforms including Windows, Linux and MacOS.
There is no restriction about what Python version your project is using, but installing PDM itself needs Python 3.7+.
Recommended installation method#
PDM requires python version 3.7 or higher.
Like Pip, PDM provides an installation script that will install PDM into an isolated environment.
curl -sSL https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py | python -
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py -UseBasicParsing).Content | python -
The installer will install PDM into the user site and the location depends on the system:
$HOME/.local/bin
for Unix%APPDATA%\Python\Scripts
on Windows
You can pass additional options to the script to control how PDM is installed:
usage: install-pdm.py [-h] [-v VERSION] [--prerelease] [--remove] [-p PATH] [-d DEP]
optional arguments:
-h, --help show this help message and exit
-v VERSION, --version VERSION | envvar: PDM_VERSION
Specify the version to be installed, or HEAD to install from the main branch
--prerelease | envvar: PDM_PRERELEASE Allow prereleases to be installed
--remove | envvar: PDM_REMOVE Remove the PDM installation
-p PATH, --path PATH | envvar: PDM_HOME Specify the location to install PDM
-d DEP, --dep DEP | envvar: PDM_DEPS Specify additional dependencies, can be given multiple times
You can either pass the options after the script or set the env var value.
Other installation methods#
brew install pdm
scoop bucket add frostming https://github.com/frostming/scoop-frostming.git
scoop install pdm
pipx install pdm
Install the head version of GitHub repository. Make sure you have installed Git LFS on your system.
pipx install git+https://github.com/pdm-project/pdm.git@main#egg=pdm
See also: https://pypa.github.io/pipx/
pip install --user pdm
Enable PEP 582 globally#
To make the Python interpreters aware of PEP 582 packages, one need to add the pdm/pep582/sitecustomize.py
to the Python library search path.
For Windows users#
One just needs to execute pdm --pep582
, then environment variable will be changed automatically. Don't forget
to restart the terminal session to take effect.
For Mac and Linux users#
The command to change the environment variables can be produced by pdm --pep582 [<SHELL>]
. If <SHELL>
isn't given, PDM will pick one based on some guesses.
You may want to write a line in your .bash_profile
(or similar profiles) to make it effective when login.
For example, in bash you can do this:
pdm --pep582 >> ~/.bash_profile
Once again, Don't forget to restart the terminal session to take effect.
Use the docker image#
PDM also provides a docker image to ease your deployment flow, to use it, write a Dockerfile with following content:
FROM frostming/pdm
COPY . /app
# -- Replace with the correct path to your app's main executable
CMD ["pdm", "run", "python", "main.py"]
Shell Completion#
PDM supports generating completion scripts for Bash, Zsh, Fish or Powershell. Here are some common locations for each shell:
pdm completion bash > /etc/bash_completion.d/pdm.bash-completion
# Make sure ~/.zfunc is added to fpath, before compinit.
pdm completion zsh > ~/.zfunc/_pdm
Oh-My-Zsh:
mkdir $ZSH_CUSTOM/plugins/pdm
pdm completion zsh > $ZSH_CUSTOM/plugins/pdm/_pdm
Then make sure pdm plugin is enabled in ~/.zshrc
pdm completion fish > ~/.config/fish/completions/pdm.fish
# Create a directory to store completion scripts
mkdir $PROFILE\..\Completions
echo @'
Get-ChildItem "$PROFILE\..\Completions\" | ForEach-Object {
. $_.FullName
}
'@ | Out-File -Append -Encoding utf8 $PROFILE
# Generate script
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
pdm completion powershell | Out-File -Encoding utf8 $PROFILE\..\Completions\pdm_completion.ps1
Unicode and ANSI supports#
PDM provides a fancy terminal UI with the help of ANSI characters and unicode emojis.
It can turn on/off automatically depending on whether it is supported on your terminal.
However, if you see any garbled characters, set env var DISABLE_UNICODE_OUTPUT=1
to turn off it.
Use with IDE#
Now there are not built-in support or plugins for PEP 582 in most IDEs, you have to configure your tools manually.
PDM will write and store project-wide configurations in .pdm.toml
and you are recommended to add following lines
in the .gitignore
:
.pdm.toml
__pypackages__/
PyCharm#
Mark __pypackages__/<major.minor>/lib
as Sources Root.
Additionally, if you want to use tools from the environment (e.g. pytest
), you have to add the
__pypackages__/<major.minor>/bin
directory to the PATH
variable in the corresponding
run/debug configuration.
VSCode#
Add the following two entries to the top-level dict in .vscode/settings.json
:
{
"python.autoComplete.extraPaths": ["__pypackages__/<major.minor>/lib"],
"python.analysis.extraPaths": ["__pypackages__/<major.minor>/lib"]
}
Enable PEP582 globally, and make sure VSCode runs using the same user and shell you enabled PEP582 for.
Cannot enable PEP582 globally?
Task Provider#
In addition, there is a VSCode Task Provider extension available for download.
This makes it possible for VSCode to automatically detect pdm scripts so they can be run natively as VSCode Tasks.
Seek for other IDEs or editors#
PDM Eco-system#
Awesome PDM is a curated list of awesome PDM plugins and resources.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK