python poetry 入门教程(代码依赖管理) | python优质外文翻译 | python 技术论坛-金年会app官方网

poetry 是一个python依赖项管理工具。

这里提到了开发 poetry 的主要原因 .

python中的打包系统和依赖性管理相当复杂,对于新手来说很难理解。即使对于经验丰富的开发人员,有时创建python项目所需的所有文件也可能很麻烦:setup.py, requirements.txt, setup.cfg, manifest.in 和新添加的 pipfile.

确实有很多我们应该考虑的文件,例如:

  • setup.py
  • requirements.txt
  • setup.cfg
  • manifest.in
  • pipfile and pipfile.lock (pipenv)

为了解决这种混乱的情况,poetry 提供了一个 pyproject.toml 文件来管理所有依赖项。

接下来,我们将对其进行设置!

要求

  • python 2.7 or 3.4 。

我将在本文中使用python 3.6.0。

$ python --version
python 3.6.0

下载安装

通过提供的安装程序安装poetry。

$ curl -ssl https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
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/.poetry/bin
this path will then be added to your `path` environment variable by
modifying the profile files located at:
$home/.profile
$home/.bash_profile
you can uninstall at any time with `poetry self:uninstall`,
or by executing this script with the --uninstall option,
and these changes will be reverted.
installing version: 0.12.12
  - downloading poetry-0.12.12-darwin.tar.gz (7.23mb)
poetry (0.12.12) is installed now. great!
to get started you need poetry's bin directory ($home/.poetry/bin) in your `path`
environment variable. next time you log in this will be done
automatically.
to configure your current shell run `source $home/.poetry/env`

要激活 poetry 命令,请运行以下命令:

source $home/.poetry/env

现在,poetry命令应该可用。让我们检查一下安装的 poetry 版本。

$ poetry —version
poetry 0.12.12

成功!

创建模板

首先,我将创建一个演示应用程序。

poetry new poetry-demo

项目结构是这样的。

$ cd poetry-demo
$ tree
.
├── readme.rst
├── poetry_demo
│   └── __init__.py
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_poetry_demo.py

让我们来看看 pyproject.toml

[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["[your name] <[your email]>"]
[tool.poetry.dependencies]
python = "^3.6"
[tool.poetry.dev-dependencies]
pytest = "^3.0"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

添加依赖

我可以直接在 pyproject.toml 文件中指定依赖项,但是使用 add 命令看起来很容易。

poetry add pendulum

自动将 pendulum 添加到 pyproject.toml 文件中。

[tool.poetry.dependencies]
python = "^3.6"
pendulum = "^2.0"

此外,还会创建 poetry.lock

$ tree
.
├── readme.rst
├── poetry.lock
├── poetry_demo
│   └── __init__.py
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_poetry_demo.py

由于这只是首次尝试使用 poetry,因此我将继续使用它,如果发现有用的东西,可能会再次写一篇博客文章 :)

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系金年会app官方网。

原文地址:

译文地址:https://learnku.com/python/t/38708

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
讨论数量: 1

果然很入门,我还没高潮呢...就结束了...

3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图