33.5k Star开源神器:让Python依赖管理从此轻松!

告别繁琐,Poetry以优雅的方式管理你的Python项目依赖
项目地址: https://github.com/python-poetry/poetry
Poetry 是一款用于声明、管理和安装 Python 项目依赖的工具,它可以确保你在任何地方都拥有正确的堆栈。它旨在取代传统的 setup.py
、requirements.txt
、setup.cfg
、MANIFEST.in
和 Pipfile
,取而代之的是一个简单的基于 pyproject.toml
的项目格式。

Poetry 解决了什么痛点?
- 依赖管理复杂: 传统方式需要手动维护多个文件,容易出错且难以管理。Poetry 将所有依赖信息集中在
pyproject.toml
文件中,方便管理和维护。 - 版本冲突: 不同的项目可能依赖于相同库的不同版本,容易导致冲突。Poetry 可以自动解决版本冲突,确保项目依赖的稳定性。
- 环境一致性: 在不同的开发环境和部署环境之间保持依赖一致性是一个挑战。Poetry 通过锁定依赖版本,确保在任何环境中都能复现相同的依赖关系。
Poetry 有什么创新之处?
- 基于
pyproject.toml
: 采用 PEP 518 标准,使用pyproject.toml
文件作为项目配置文件,符合行业趋势。 - 依赖分组: 支持依赖分组,可以将开发依赖、文档依赖等进行分组管理,方便安装和卸载。
- 插件系统: 拥有丰富的插件生态系统,可以扩展 Poetry 的功能,例如导出 requirements.txt 文件、打包到虚拟环境等。
pyproject.toml
示例:
[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 通过简洁的配置、强大的依赖管理和丰富的插件生态,极大地简化了 Python 项目的依赖管理流程。 无论是个人开发者还是团队协作,Poetry 都能显著提升开发效率。
怎么样,看完这个项目是不是觉得很棒? 这个项目对于 Python 开发者来说非常有用! 快来转发给你的朋友们一起学习使用吧!