How to Manage Your Python Project Using Poetry
· 8 min read
前段时间在写 tidbcloudy(一个 TiDB Cloud Python SDK)的时候用到了 Poetry 来管理依赖以及发布到 PyPI。经历了五次小版本的迭代,感觉 Poetry 时常让我有一种“写 SDK 也不难,发版也可以很简单”的错觉,所以决定记录一下我是如何使用 Poetry 来完成我的第一个 SDK 的。
Poetry 是什么
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Poetry offers a lockfile to ensure repeatable installs, and can build your project for distribution.
Poetry 是一个管理 Python 依赖的工具,它可以帮助你声明你的项目依赖的库,然后帮你管理(安装/更新)它们。Poetry 提供了一个 poetry.lock
文件来确保可复现,也可以将你的项目打包。
安装 Poetry
下面以在 Ubuntu 20.04 上安装及使用 Poetry 为例。详细的安装方法,请参考 Poetry 官方文档。
curl -sSL https://install.python-poetry.org | python3 -
输出结果示例:
Retrieving Poetry metadata
# Welcome to Poetry!
This will download and install the latest version of Poetry,
a dependency and package manager for Python.
It will add the `poetry` command to Poetry's bin directory, located at:
/home/oreo/.local/bin
You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.
Installing Poetry (1.2.2): Creating environment
Installing Poetry (1.2.2): Done
Poetry (1.2.2) is installed now. Great!
You can test that everything is set up by executing:
`poetry --version`
查看 Poetry 版本:
poetry --version
输出结果示例:
Poetry (version 1.2.2)