现代Python打包工具链
现代Python打包工具如Poetry、Flit和Hatch提供了更简单、更强大的方式来管理项目依赖和打包流程。下面我将通过具体示例详细介绍这三种工具。
1. Poetry - 全功能依赖管理工具
Poetry是最流行的现代Python项目管理工具之一,它集依赖管理、虚拟环境管理和打包发布于一体。
典型项目结构
my_poetry_project/
├── pyproject.toml
├── README.md
├── src/
│ └── my_package/
│ ├── __init__.py
│ └── module.py
└── tests/
pyproject.toml 示例
[tool.poetry]
name = "my-poetry-project"
version = "0.1.0"
description = "A project managed by Poetry"
authors = ["Your Name <you@example.com>"]
license = "MIT"
readme = "README.md"[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.26.0"
numpy = { version = "^1.21.0", optional = true }[tool.poetry.dev-dependencies]
pytest = "^6.2.0"
black = "^21.0"
mypy = "^0.910"[tool.poetry.extras]
full = ["numpy"][tool.poetry.scripts]
mycli = "my_package.cli:main"[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
常用命令
# 初始化新项目
poetry new my-project# 添加依赖
poetry add requests
poetry add --dev pytest# 安装所有依赖
poetry install# 安装带可选依赖
poetry install --extras "full"# 运行命令
poetry run python -m pytest# 构建包
poetry build# 发布到PyPI
poetry publish
2. Flit - 简单项目的轻量级工具
Flit专注于简化小型包的发布流程,特别适合单模块包。
典型项目结构
my_flit_project/
├── pyproject.toml
├── README.md
└── my_module.py # 或 src/目录结构
pyproject.toml 示例
[build-system]
requires = ["flit_core>=3.2"]
build-backend = "flit_core.buildapi"[project]
name = "my-flit-module"
authors = [{name = "Your Name", email = "you@example.com"},
]
version = "0.1.0"
description = "A simple module packaged with Flit"
requires-python = ">=3.6"
classifiers = ["License :: OSI Approved :: MIT License",
]
dependencies = ["requests>=2.25.0",
][project.optional-dependencies]
test = ["pytest>=6.0","pytest-cov>=2.0",
][project.urls]
Homepage = "https://example.com"
常用命令
# 初始化项目(交互式)
flit init# 构建包
flit build# 发布到PyPI
flit publish# 安装开发模式
flit install --symlink
3. Hatch - 新一代项目管理工具
Hatch是一个相对较新但功能强大的工具,提供了统一的项目管理和构建体验。
典型项目结构
my_hatch_project/
├── pyproject.toml
├── README.md
├── src/
│ └── my_package/
│ ├── __init__.py
│ └── module.py
└── tests/
pyproject.toml 示例
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"[project]
name = "my-hatch-project"
version = "0.1.0"
description = "A project managed by Hatch"
authors = [{ name="Your Name", email="you@example.com" },
]
readme = "README.md"
requires-python = ">=3.7"
license = "MIT"
keywords = ["example", "hatch"]
classifiers = ["Development Status :: 3 - Alpha","Intended Audience :: Developers",
]dependencies = ["click>=8.0.0","rich>=10.0.0",
][project.optional-dependencies]
dev = ["pytest>=6.0","pytest-mock>=3.0",
]
test = ["coverage>=5.0",
][project.scripts]
myapp = "my_package.cli:main"[tool.hatch.envs.default]
dependencies = ["pytest","pytest-cov",
]
常用命令
# 创建新项目
hatch new my-project# 添加依赖
hatch env add python=3.9# 运行测试
hatch run test# 构建包
hatch build# 发布包
hatch publish# 版本管理
hatch version # 显示当前版本
hatch version patch # 升级补丁版本
hatch version minor # 升级次版本
工具对比
特性
Poetry
Flit
Hatch
依赖管理
✓
✓
✓
虚拟环境管理
✓
✗
✓
版本管理
✓
✗
✓
复杂项目支持
✓
✗
✓
单文件项目支持
✗
✓
✗
构建系统
✓
✓
✓
发布到PyPI
✓
✓
✓
多环境支持
有限
✗
✓
插件系统
✓
✗
✓
选择建议
1. 选择Poetry如果:
◦ 你需要完整的依赖管理解决方案
◦ 项目有复杂的依赖关系
◦ 需要与现有工具链良好集成
2. 选择Flit如果:
◦ 你的项目非常简单(特别是单文件模块)
◦ 你想要最简化的配置
◦ 你不需要虚拟环境管理
3. 选择Hatch如果:
◦ 你想要最新的工具和功能
◦ 需要强大的环境管理
◦ 项目可能有多种配置或构建变体
这些现代工具都使用pyproject.toml作为配置文件,符合PEP 517和PEP 518标准,使得项目更容易在不同工具间迁移。