如何在 Fedora 上使用 Poetry 来管理你的 Python 项目？
Python
![Python & Poetry on Fedora](/data/attachment/album/202103/14/065917wquzrh2orvvug4mn.jpg)


Python 开发人员经常创建一个新的虚拟环境来分离项目依赖，然后用 `pip`、`pipenv` 等工具来管理它们。Poetry 是一个简化 Python 中依赖管理和打包的工具。这篇文章将向你展示如何在 Fedora 上使用 Poetry 来管理你的 Python 项目。


与其他工具不同，Poetry 只使用一个配置文件来进行依赖管理、打包和发布。这消除了对不同文件的需求，如 `Pipfile`、`MANIFEST.in`、`setup.py` 等。这也比使用多个工具更快。


下面详细介绍一下开始使用 Poetry 时使用的命令。


### 在 Fedora 上安装 Poetry


如果你已经使用 Fedora 32 或以上版本，你可以使用这个命令直接从命令行安装 Poetry：



```
$ sudo dnf install poetry

```

编者注：在 Fedora Silverblue 或 CoreOs上，Python 3.9.2 是核心提交的一部分，你可以用下面的命令安装 Poetry：



```
rpm-ostree install poetry

```

### 初始化一个项目


使用 `new` 命令创建一个新项目：



```
$ poetry new poetry-project

```

用 Poetry 创建的项目结构是这样的：



```
├── poetry_project
│   └── init.py
├── pyproject.toml
├── README.rst
└── tests
    ├── init.py
    └── test_poetry_project.py

```

Poetry 使用 `pyproject.toml` 来管理项目的依赖。最初，这个文件看起来类似于这样：



```
[tool.poetry]
name = "poetry-project"
version = "0.1.0"
description = ""
authors = ["Kadermiyanyedi <kadermiyanyedi@hotmail.com>"]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

```

这个文件包含 4 个部分：


* 第一部分包含描述项目的信息，如项目名称、项目版本等。
* 第二部分包含项目的依赖。这些依赖是构建项目所必需的。
* 第三部分包含开发依赖。
* 第四部分描述的是符合 [PEP 517](https://www.python.org/dev/peps/pep-0517/) 的构建系统。


如果你已经有一个项目，或者创建了自己的项目文件夹，并且你想使用 Poetry，请在你的项目中运行 `init` 命令。



```
$ poetry init

```

在这个命令之后，你会看到一个交互式的 shell 来配置你的项目。


### 创建一个虚拟环境


如果你想创建一个虚拟环境或激活一个现有的虚拟环境，请使用以下命令：



```
$ poetry shell

```

Poetry 默认在 `/home/username/.cache/pypoetry` 项目中创建虚拟环境。你可以通过编辑 Poetry 配置来更改默认路径。使用下面的命令查看配置列表：



```
$ poetry config --list

cache-dir = "/home/username/.cache/pypoetry"
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.path = "{cache-dir}/virtualenvs"

```

修改 `virtualenvs.in-project` 配置变量，在项目目录下创建一个虚拟环境。Poetry 命令是：



```
$ poetry config virtualenv.in-project true

```

### 添加依赖


使用 `poetry add` 命令为项目安装一个依赖：



```
$ poetry add django

```

你可以使用带有 `--dev` 选项的 `add` 命令来识别任何只用于开发环境的依赖：



```
$ poetry add black --dev

```

`add` 命令会创建一个 `poetry.lock` 文件，用来跟踪软件包的版本。如果 `poetry.lock` 文件不存在，那么会安装 `pyproject.toml` 中所有依赖项的最新版本。如果 `poetry.lock` 存在，Poetry 会使用文件中列出的确切版本，以确保每个使用这个项目的人的软件包版本是一致的。


使用 `poetry install` 命令来安装当前项目中的所有依赖：



```
$ poetry install

```

通过使用 `--no-dev` 选项防止安装开发依赖：



```
$ poetry install --no-dev

```

### 列出软件包


`show` 命令会列出所有可用的软件包。`--tree` 选项将以树状列出软件包：



```
$ poetry show --tree

django 3.1.7 A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
├── asgiref >=3.2.10,<4
├── pytz *
└── sqlparse >=0.2.2

```

包含软件包名称，以列出特定软件包的详细信息：



```
$ poetry show requests

name         : requests
version      : 2.25.1
description  : Python HTTP for Humans.

dependencies
 - certifi >=2017.4.17
 - chardet >=3.0.2,<5
 - idna >=2.5,<3
 - urllib3 >=1.21.1,<1.27

```

最后，如果你想知道软件包的最新版本，你可以通过 `--latest` 选项：



```
$ poetry show --latest

idna     2.10      3.1    Internationalized Domain Names in Applications
asgiref  3.3.1     3.3.1  ASGI specs, helper code, and adapters

```

### 更多信息


Poetry 的更多详情可在[文档](https://python-poetry.org/docs/)中获取。




---


via: <https://fedoramagazine.org/how-to-use-poetry-to-manage-your-python-projects-on-fedora/>


作者：[Kader Miyanyedi](https://fedoramagazine.org/author/moonkat/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Python developers often create a new virtual environment to separate project dependencies and then manage them with tools such as *pip, pipenv, etc.* Poetry is a tool for simplifying dependency management and packaging in Python. This post will show you how to use Poetry to manage your Python projects on Fedora.

Unlike other tools, Poetry uses only a single configuration file for dependency management, packaging, and publishing. This eliminates the need for different files such as *Pipfile, MANIFEST.in, setup.py*, etc. It is also faster than* *using multiple tools.

Detailed below is a brief overview of commands used when getting started with Poetry.

**Installing Poetry on Fedora**

If you already use Fedora 32 or above, you can install Poetry directly from the command line using this command:

$ sudo dnf install poetry

*Editor note: on Fedora Silverblue or CoreOs Python 3.9.2 is part of the core commit, you would layer Poetry with ‘*rpm-ostree install poetry*‘*

## Initialize a project

Create a new project using the* new *command.

$ poetry new poetry-project

The structure of a project created with Poetry looks like this:

├── poetry_project │ └──init.py ├── pyproject.toml ├── README.rst └── tests ├──init.py └── test_poetry_project.py

Poetry uses *pyproject.toml *to manage the dependencies of your project. Initially, this file will look similar to this:

[tool.poetry] name = "poetry-project" version = "0.1.0" description = "" authors = ["Someone <someone@somemail.com>"] [tool.poetry.dependencies] python = "^3.9" [tool.poetry.dev-dependencies] pytest = "^5.2" [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"

This file contains 4 sections:

- The first section contains information describing the project such as project name, project version, etc.
- The second section contains project dependencies. These dependencies are necessary to build the project.
- The third section contains development dependencies.
- The fourth section describes a building system as in
[PEP 517](https://www.python.org/dev/peps/pep-0517/)

If you already have a project, or create your own project folder, and you want to use poetry, run the *init* command within your project.

$ poetry init

After this command, you will see an interactive shell to configure your project.

## Create a virtual environment

If you want to create a virtual environment or activate an existing virtual environment, use the command below:

$ poetry shell

Poetry creates the virtual environment in the* /home/username/.cache/pypoetry* project by default. You can change the default path by editing the poetry config. Use the command below to see the config list:

$ poetry config --listcache-dir = "/home/username/.cache/pypoetry" virtualenvs.create = true virtualenvs.in-project = true virtualenvs.path = "{cache-dir}/virtualenvs"

Change the *virtualenvs.in-project* configuration variable to create a virtual environment within your project directory. The Poetry command is:

$ poetry config virtualenvs.in-project true

## Add dependencies

Install a dependency for the project with the* poetry add *command.

$ poetry add django

You can identify any dependencies that you use only for the development environment using the *add* command with the *–dev* option.

$ poetry add black --dev

The **add **command creates a *poetry.lock file* that is used to track package versions. If the *poetry.lock* file doesn’t exist, the latest versions of all dependencies in *pyproject.toml* are installed. If *poetry.lock* does exist, Poetry uses the exact versions listed in the file to ensure that the package versions are consistent for everyone working on your project.

Use the poetry *install* command to install all dependencies in your current project.

$ poetry install

Prevent development dependencies from being installed by using the *no-dev *option.

$ poetry install --no-dev

## List packages

The *show* command lists all of the available packages. The *tree* option will list packages as a tree.

$ poetry show --treedjango3.1.7A high-level Python Web framework that encourages rapid development and clean, pragmatic design. ├── asgiref >=3.2.10,<4 ├── pytz * └── sqlparse >=0.2.2

Include the package name to list details of a specific package.

$ poetry show requestsname : requests version :2.25.1description : Python HTTP for Humans. dependencies - certifi>=2017.4.17- chardet>=3.0.2,<5- idna>=2.5,<3- urllib3>=1.21.1,<1.27

Finally, if you want to learn the latest version of the packages, you can pass the *latest* option.

$ poetry show --latestidna2.103.1 Internationalized Domain Names in Applications asgiref3.3.13.3.1 ASGI specs, helper code, and adapters

## Further information

More details on Poetry are available in the [documentation](https://python-poetry.org/docs/).

## Artur

Small tip: you can use dephell (https://dephell.readthedocs.io/) to change from other formats like requirements.txt or Pipfile to poetry.

Install dephell, add below to pyproject.toml and run “dephell deps convert”.

[tool.dephell.main]

from = {format = “pip”, path = “requirements.txt”}

to = {format = “poetry”, path = “pyproject.toml”}

## Ryan

This is nifty!

Poetry does have the ability to export a requirements.txt file natively(poetry export -f requirements.txt), if this is all that is desired.

## Katsuharu Tanaka

Thank you for introducing a useful tool. I will try it in a future project.

## Daniel Eguia

Nice write up… Will try this out soon

## Tim Hughes

Nice and clearly explained. It may also be worth doing a followup on this article to include building as a wheel and publishing to pypi as a project.

I use poetry for all my new projects so I built a cookiecutter template for it which others may find useful (or not).

– https://github.com/timhughes/cookiecutter-poetry

## besiw93427

Nice.

Maybe I’ll give this a try.

## ojnny

Thank you! Found a typo here:

poetry config virtualenv.in-project true

should be:

poetry config virtualenvs.in-project true

## Ryan

A shortcut for adding requirements for an existing project is something like:

poetry add $(cat requirements.txt)

Has definitely come in handy for us.

## thunderbirdtr

Thank you for the nice article, It is a very hand tool as well.