pdm的使用

安装:

pip install pdm
windows平台可能会有问题,需要手动添加到环境变量~

在项目中使用:

mkdir my-project && cd my-project
pdm init
根据提示,创建相关的文件~
  1. 选择python解释器(如果有多个python解释器)
  2. 是否创建虚拟环境. (如果选择是,会在当前目录下创建一个虚拟环境,并使用虚拟环境的python解释器)
如果所选的 Python 解释器位于虚拟环境中,PDM 将将其用作项目环境并安装依赖项 进入它。否则,将在项目根目录中创建,并将依赖项安装到其中
  1. 安装库: pdm add flask, 卸载 pdm remove xxx
  2. 版本控制:  建议提交*.toml文件,. ( 项目的构建元数据和 PDM 所需的依赖关系)
  3. 从其他包管理器导入项目(略)

管理依赖关系

添加依赖

pdm add  后面可以跟着一个或多个依赖项,依赖项规范在  PEP 508  中描述。
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>
  1. * reuse:保留所有锁定的依赖项,但命令行中给出的依赖项除外(默认)。
  2. * reuse-installed:尝试重复使用工作集中安装的版本。这也将影响命令行中请求的包。
  3. * eager:尝试在命令行中锁定较新版本的包及其递归子依赖项,并保持其他依赖项不变。
  4. * 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

  • 18
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白日与明月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值