Python Poetry 依赖管理工具

发布时间 2023-08-13 16:19:17作者: 软匠

Python 依赖管理工具 poetry

安装

Linux, macOS, Windows (WSL)

curl -sSL https://install.python-poetry.org | python3 -

Windows (Powershell)

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py

配置

使用 poetry config --list 来查看配置

PS C:\Users\lingh\Desktop\poetry-demo> poetry config --list
cache-dir = "C:\\Users\\lingh\\AppData\\Local\\pypoetry\\Cache"
experimental.system-git-client = false
installer.max-workers = null
installer.modern-installation = true
installer.no-binary = null
installer.parallel = true
repositories.tsinghua.url = "https://pypi.tuna.tsinghua.edu.cn/simple"
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}\\virtualenvs"  # C:\Users\lingh\AppData\Local\pypoetry\Cache\virtualenvs
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{project_name}-py{python_version}"
PS C:\Users\lingh\Desktop\poetry-demo>

默认 poetry 会在 {cache-dir}\virtualenvs 创建虚拟环境,可以修改为创建在每个项目的目录下

PS C:\Users\lingh\Desktop\poetry-demo> poetry config virtualenvs.in-project true
PS C:\Users\lingh\Desktop\poetry-demo>

新建项目

使用 poetry new 来创建项目

poetry new poetry-demo

进入项目目录

cd poetry-demo

添加清华源,这里还需要添加 pypi 不然会有一个 warning

poetry source add tsinghua https://pypi.tuna.tsinghua.edu.cn/simple
poetry source add pypi

添加依赖,可以分组

poetry add requests
poetry add pytest --group test

使用 pycharm 打开项目

pycharm 会自动检测并使用项目目录下 .venv 的解释器,如果不是,需要手动配置一下

配置 pycharm 使用 pytest 进行测试
Testing -> Default test runner 选 pytest

这样就可以执行测试了

也可以使用 poetry 来执行测试

poetry run pytest

Poetry 依赖管理

poetry 通过 group 来进行依赖管理
运行时依赖放在 [tool.poetry.dependencies] 下面
测试需要的依赖可以放在 [tool.poetry.group.test.dependencies] 下面

[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.31.0"


[tool.poetry.group.test.dependencies]
pytest = "^7.4.0"

开发需要的依赖可以放在 [tool.poetry.group.dev.dependencies] 下面

[tool.poetry.group.dev.dependencies]
pytest = "^6.0.0"
pytest-mock = "*"

使用 optional 来指定可选依赖,只有在明确指定时才会去下载

[tool.poetry.group.docs]
optional = true

[tool.poetry.group.docs.dependencies]
mkdocs = "*"

使用 --with 下载可选依赖

poetry install --with docs

添加依赖

不加 group 时添加到 [tool.poetry.dependencies] 下面

poetry add requests

使用 --group 来添加到对应的分组下

poetry add pytest --group test

安装依赖

poetry install

如果有可选依赖需要通过 --with 来安装

poetry install --with test,docs

删除依赖

poetry remove requests
poetry remove mkdocs --group docs

poetry 命令列表

项目命令

创建项目

poetry new my-package

创建的项目结构如下

my-package
├── pyproject.toml
├── README.md
├── src
│   └── my_package
│       └── __init__.py
└── tests
    └── __init__.py

现有项目 使用 poetry
在项目根目录执行 poetry init 会生成 pyproject.toml 之后就可以采用 poetry 来进行依赖管理

poetry init

依赖命令

安装依赖

poetry install

更新依赖
默认时 poetry 会使用 poetry.lock 文件锁定依赖库的版本,需要更新时执行 poetry update

poetry update

添加依赖

poetry add requests pendulum

添加依赖时指定版本约束

# Allow >=2.0.5, <3.0.0 versions
poetry add pendulum@^2.0.5

# Allow >=2.0.5, <2.1.0 versions
poetry add pendulum@~2.0.5

# Allow >=2.0.5 versions, without upper bound
poetry add "pendulum>=2.0.5"

# Allow only 2.0.5 version
poetry add pendulum==2.0.5

可以添加 git 私有依赖

poetry add git+https://github.com/sdispater/pendulum.git
poetry add git+https://github.com/sdispater/pendulum.git#develop
poetry add git+https://github.com/sdispater/pendulum.git#2.0.5

删除依赖

poetry remove pendulum

显示依赖

PS C:\Users\lingh\Desktop\poetry-demo> poetry show
certifi            2023.7.22 Python package for providing Mozilla's CA Bundle.
charset-normalizer 3.2.0     The Real First Universal Charset Detector. Open, modern and actively maintained alter...
colorama           0.4.6     Cross-platform colored terminal text.
exceptiongroup     1.1.2     Backport of PEP 654 (exception groups)
idna               3.4       Internationalized Domain Names in Applications (IDNA)
iniconfig          2.0.0     brain-dead simple config-ini parsing
packaging          23.1      Core utilities for Python packages
pluggy             1.2.0     plugin and hook calling mechanisms for python
pytest             7.4.0     pytest: simple powerful testing with Python
requests           2.31.0    Python HTTP for Humans.
tomli              2.0.1     A lil' TOML parser
urllib3            2.0.4     HTTP library with thread-safe connection pooling, file post, and more.
PS C:\Users\lingh\Desktop\poetry-demo>

搜索依赖

PS C:\Users\lingh\Desktop\poetry-demo> poetry search requests

requests (2.31.0)
 Python HTTP for Humans.

导出依赖,可以导出 pip 格式的依赖

poetry export -f requirements.txt --output requirements.txt

配置命令

配置

poetry config --list

修改配置

poetry config virtualenvs.in-project true

运行命令

在虚拟环境里面执行

poetry run python -V

缓存

列出缓存

poetry cache list

清除缓存

poetry cache clear pypi --all

添加源

poetry source add tsinghua https://pypi.tuna.tsinghua.edu.cn/simple

添加源时指定优先级 --priority

poetry source add --priority=explicit pypi

源的优先顺序

  • default
  • primary
  • implicit PyPI (当有 default 或被指定为 explicit 时无效)
  • supplemental sources
  • explicit 只有依赖指定这个源名称时,才会从 explicit 源下载

添加依赖时指定源名称

poetry add --source internal-pypi httpx

显示源

poetry source show

显示依赖的源

poetry source show pypi-test

删除源

poetry source remove pypi-test
---- ---- ----