Python Poetry 代码依赖管理

Python Poetry 代码依赖管理

Poetry 的历史

Poetry 是一个 Python 依赖项管理工具。

这里提到了开发 Poetry 的主要原因 项目 readme.

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 文件来管理所有依赖项。

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

配置 Poetry

要求

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 演示

创建模板

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

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 add flask  # 安装最新稳定版本的flask

poetry add pytest --dev  # 指定为开发依赖,会写到pyproject.toml中的[tool.poetry.dev-dependencies]区域

poetry add flask=2.22.0  # 指定具体的版本

poetry install  # 安装pyproject.toml文件中的全部依赖

poetry install --no-dev  # 只安装非development环境的依赖,一般部署时使用<br />

poetry update  #更新所有锁定版本的依赖包

poetry update numpy  # 更新指定依赖包

poetry remove numpy  # 卸载依赖包

poetry show --outdated  # 查看可以更新的依赖

poetry show  # 查看项目安装的依赖

poetry show -t  # 树形结构查看项目安装的依赖

虚拟环境管理

创建虚拟环境

创建虚拟环境有2种方式:

方式1:

如果在配置文件中配置了virtualenvs.create=true,执行poetry install时会检查是否有虚拟环境,否则会自动创建。

方式2:

# 指定创建虚拟环境时使用的Python解释器版本
$ poetry env use python3.7

命令参数详解

poetry shell  # 激活虚拟环境

poetry env info  # 查看虚拟环境信息

poetry env list  # 显示虚拟环境列表

poetry env list --full-path  # 显示虚拟环境绝对路径

poetry env remove python3.7  # 删除虚拟环境

poetry run python -V  # 查看python版本

本文为转载文章,贵在分享,版权归原作者及原出处所有,如涉及版权等问题,请及时与我联系。
原文出处:https://dev.to/yukinagae/beginner-guide-on-poetry-new-python-dependency-management-tool-4327
原文链接:https://learnku.com/python/t/38708

(3)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
兰玉磊兰玉磊
上一篇 2020年10月23日
下一篇 2020年11月23日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注