translating

This commit is contained in:
geekpi 2021-03-11 08:45:29 +08:00
parent ebfe24391b
commit 92fc9ccfc8
2 changed files with 205 additions and 206 deletions

View File

@ -1,206 +0,0 @@
[#]: subject: (How to use Poetry to manage your Python projects on Fedora)
[#]: via: (https://fedoramagazine.org/how-to-use-poetry-to-manage-your-python-projects-on-fedora/)
[#]: author: (Kader Miyanyedi https://fedoramagazine.org/author/moonkat/)
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
How to use Poetry to manage your Python projects on Fedora
======
![Python & Poetry on Fedora][1]
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 = ["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"
```
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][2]
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 --list
cache-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 virtualenv.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 doesnt 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 --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
```
Include the package name to list details of a specific package.
```
$ 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
```
Finally, if you want to learn the latest version of the packages, you can pass the _latest_ option.
```
$ 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
```
### Further information
More details on Poetry are available in the [documentation][3].
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/how-to-use-poetry-to-manage-your-python-projects-on-fedora/
作者:[Kader Miyanyedi][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/moonkat/
[b]: https://github.com/lujun9972
[1]: https://fedoramagazine.org/wp-content/uploads/2021/03/Poetry_Python-816x345.jpg
[2]: https://www.python.org/dev/peps/pep-0517/
[3]: https://python-poetry.org/docs/

View File

@ -0,0 +1,205 @@
[#]: subject: (How to use Poetry to manage your Python projects on Fedora)
[#]: via: (https://fedoramagazine.org/how-to-use-poetry-to-manage-your-python-projects-on-fedora/)
[#]: author: (Kader Miyanyedi https://fedoramagazine.org/author/moonkat/)
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
如何在 Fedora 上使用 Poetry 来管理你的 Python 项目?
======
![Python & Poetry on Fedora][1]
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
```
The structure of a project created with Poetry looks like this:
用 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][2] 的构建系统。
如果你已经有一个项目,或者创建了自己的项目文件夹,并且你想使用 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 的更多详情可在[文档][3]中获取。
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/how-to-use-poetry-to-manage-your-python-projects-on-fedora/
作者:[Kader Miyanyedi][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/moonkat/
[b]: https://github.com/lujun9972
[1]: https://fedoramagazine.org/wp-content/uploads/2021/03/Poetry_Python-816x345.jpg
[2]: https://www.python.org/dev/peps/pep-0517/
[3]: https://python-poetry.org/docs/