介绍
Poetry 用一个pyproject.toml 代替 setup.py, requirements.txt, setup.cfg, MANIFEST.in and Pipfile文件。
用过vue的肯定看上去会无比熟悉,类似于package.json文件。
[tool.poetry]
name = "my-package"
version = "0.1.0"
description = "The description of the package"
license = "MIT"
authors = [
"Sébastien Eustace <sebastien@eustace.io>"
]
repository = "https://github.com/python-poetry/poetry"
homepage = "https://python-poetry.org"
# README file(s) are used as the package description
readme = ["README.md", "LICENSE"]
# Keywords (translated to tags on the package index)
keywords = ["packaging", "poetry"]
[tool.poetry.dependencies]
# Compatible Python versions
python = ">=3.8"
# Standard dependency with semver constraints
aiohttp = "^3.8.1"
# Dependency with extras
requests = { version = "^2.28", extras = ["security"] }
# Version-specific dependencies with prereleases allowed
tomli = { version = "^2.0.1", python = "<3.11", allow-prereleases = true }
# Git dependencies
cleo = { git = "https://github.com/python-poetry/cleo.git", branch = "main" }
# Optional dependencies (installed by extras)
pendulum = { version = "^2.1.2", optional = true }
# Dependency groups are supported for organizing your dependencies
[tool.poetry.group.dev.dependencies]
pytest = "^7.1.2"
pytest-cov = "^3.0"
# ...and can be installed only when explicitly requested
[tool.poetry.group.docs]
optional = true
[tool.poetry.group.docs.dependencies]
Sphinx = "^5.1.1"
# Python-style entrypoints and scripts are easily expressed
[tool.poetry.scripts]
my-script = "my_package:main"
常用命令
初始化项目:poetry new poetry-demo
项目结构:
poetry-demo
├── pyproject.toml
├── README.md
├── poetry_demo
│ └── __init__.py
└── tests
└── __init__.py
已有项目:poetry init
初始化pyproject.toml
文件
安装依赖:
- 方式一:
poetry add pendulum
,安装成功后[tool.poetry.group.dev.dependencies]
下可以看到 - 方式二: 修改
[tool.poetry.group.dev.dependencies]
文件内容,poetry update
查看包信息:poetry show
查看切换虚拟环境:peotry shell
查看环境信息:peotry env info
打包项目:peotry build
,默认打包目录dist/**.gz, dist/**.whl
官方文档
https://python-poetry.org/docs/