安装:
pip install pdm
windows平台可能会有问题,需要手动添加到环境变量~
在项目中使用:
mkdir my-project && cd my-projectpdm init
根据提示,创建相关的文件~
-
选择python解释器(如果有多个python解释器)
-
是否创建虚拟环境. (如果选择是,会在当前目录下创建一个虚拟环境,并使用虚拟环境的python解释器)
如果所选的 Python 解释器位于虚拟环境中,PDM 将将其用作项目环境并安装依赖项 进入它。否则,将在项目根目录中创建,并将依赖项安装到其中
-
安装库: pdm add flask, 卸载 pdm remove xxx
-
版本控制: 建议提交*.toml文件,. ( 项目的构建元数据和 PDM 所需的依赖关系)
-
从其他包管理器导入项目(略)
管理依赖关系
添加依赖
pdm add requests # add requests
pdm add requests==2.25.1 # add requests with version constraint
pdm add requests[socks] # add requests with extra dependency
pdm add "flask>=1.0" flask-sqlalchemy # add multiple dependencies with different specifiers
可以添加本地包及其路径。路径可以是文件或目录:
pdm add ./sub-package
pdm add ./first-1.0.0-py2.py3-none-any.whl
PDM 还支持直接从 Web 地址下载和安装软件包。
从git或者其他版本控制系统进行安装:
# Install pip repo on tag `22.0`
pdm add "git+https://github.com/pypa/pip.git@22.0"
# Provide credentials in the URL
pdm add "git+https://username:password@github.com/username/private-repo.git@master"
# Give a name to the dependency
pdm add "pip @ git+https://github.com/pypa/pip.git@22.0"
# Or use the #egg fragment
pdm add "git+https://github.com/pypa/pip.git@22.0#egg=pip"
# Install from a subdirectory
pdm add "git+https://github.com/owner/repo.git@master#egg=pkg&subdirectory=subpackage"
PDM 还支持定义对开发有用的依赖项组, 例如,有些用于测试,有些用于棉绒。我们通常不希望这些依赖项出现在发行版的元数据中 所以使用可能不是一个好主意。我们可以将它们定义为开发依赖项
更新现有依赖项
要更新锁定文件中的所有依赖项,请执行以下操作:
pdm update
pdm update requests
pdm update -G security -G http
pdm update -G "security,http"
pdm update -G security cryptography
# Update all default + dev-dependencies
pdm update -d
# Update a package in the specified group of dev-dependencies
pdm update -dG test pytest
关于更新策略
同样,PDM 也提供了 3 种不同的更新依赖关系和子依赖关系的行为, 这是通过选项给出的:--update-<strategy>
- * reuse:保留所有锁定的依赖项,但命令行中给出的依赖项除外(默认)。
- * reuse-installed:尝试重复使用工作集中安装的版本。这也将影响命令行中请求的包。
- * eager:尝试在命令行中锁定较新版本的包及其递归子依赖项,并保持其他依赖项不变。
- * all:更新所有依赖项和子依赖项。
删除现有依赖项
# Remove requests from the default dependencies
pdm remove requests
# Remove h11 from the 'web' group of optional-dependencies
pdm remove -G web h11
# Remove pytest-cov from the `test` group of dev-dependencies
pdm remove -dG test pytest-cov
将锁定的包导出为替代格式:
pdm export -o requirements.txt
生成和发布
发布:
pdm publish
pdm build # 单独生成
pdm publish --no-build # 发布
这将自动构建一个 wheel 和一个源代码发行版 (sdist),并将它们上传到 PyPI 索引。
项目配置
配置pypi源为国内的:
pdm config pypi.url https://pypi.tuna.tsinghua.edu.cn/simple
PDM 脚本
pdm支持加载本地包的任意脚本或命令.
pdm run flask run -p 8000
自定义脚本快捷方式
# 编辑pyproject.toml
[tool.pdm.scripts]
start = "flask run -p 54321"
# 运行~
pdm run start
pdm run start -h 0.0.0.0 # 添加额外的参数
支持四种类型的脚本:
-
cmd
-
shell
-
call
-
composite
命令行说明:
Usage: pdm [-h] [-V] [-c CONFIG] [-v | -q] [-I] [--pep582 [SHELL]] ...
Options:
-h, --help Show this help message and exit.
-V, --version Show the version and exit
-c CONFIG, --config CONFIG
Specify another config file path [env var: PDM_CONFIG_FILE]
-v, --verbose Use `-v` for detailed output and `-vv` for more detailed
-q, --quiet Suppress output
-I, --ignore-python Ignore the Python path saved in .pdm-python. [env var: PDM_IGNORE_SAVED_PYTHON]
--pep582 [SHELL] Print the command line to be eval'd by the shell
Commands:
add Add package(s) to pyproject.toml and install them
build Build artifacts for distribution
cache Control the caches of PDM
completion Generate completion scripts for the given shell
config Display the current configuration
export Export the locked packages set to other formats
fix Fix the project problems according to the latest version of PDM
import Import project metadata from other formats
info Show the project information
init Initialize a pyproject.toml for PDM
install Install dependencies from lock file
list List packages installed in the current working set
lock Resolve and lock dependencies
publish Build and publish the project to PyPI
remove Remove packages from pyproject.toml
run Run commands or scripts with local packages loaded
search Search for PyPI packages
self (plugin) Manage the PDM program itself (previously known as plugin)
show Show the package information
sync Synchronize the current working set with lock file
update Update package(s) in pyproject.toml
use Use the given python version or path as base interpreter
venv Virtualenv management