Poetry
一、 概述
1、 介绍
Poetry 是Python 中用于依赖管理和**打包的工具。**它允许您声明项目所依赖的库,并将为您管理(安装/更新)它们。Poetry 提供了一个锁定文件以确保可重复安装,并且可以构建您的项目以进行分发。
作为一个传统虚拟环境的实现,poetry 凭借其强大的依赖分析能力被大量项目所推荐的虚拟环境管理工具。
对于绝大部分的开发者而言,poetry 作为首选的虚拟环境管理工具是最优方案,其强大的依赖分析能力、环境封装能力以及庞大的社区均可为你的开发保驾护航,这也是我目前最为推荐所有 python 开发者所掌握的虚拟环境管理工具。
2、 环境配置
2.1 官方推荐
2.1.1 安装
curl -sSL https://install.python-poetry.org | python3 -
环境变量:
POETRY_HOME
:安装目录POETRY_PREVIEW
:是否安装预发布版本,如果需要,设置为POETRY_PREVIEW=1
POETRY_VERSION
:指定安装的版本--git
:从git
存储库中安装使用示例:
curl -sSL https://install.python-poetry.org | python3 - --git https://github.com/python-poetry/poetry.git@master curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.2.0 python3 -
2.1.2 配置环境变量
poetry
安装程序在一个众所周知的、特定于平台的目录中创建一个包装器:
$HOME/.local/bin
在 Unix 上%APPDATA%\Python\Scripts
在 Windows 上$POETRY_HOME/bin
如果$POETRY_HOME
设置
2.1.3 更新poetry
poetry self update # 更新
poetry self update --preview # 安装预装版
poetry self update 1.2.0 # 更新到指定的版本
2.1.4 卸载poetry
curl -sSL https://install.python-poetry.org | python3 - --uninstall
curl -sSL https://install.python-poetry.org | POETRY_UNINSTALL=1 python3 -
2.2 pip安装
pip install poetry # 安装诗歌
pipx upgrade poetry # 更新诗歌
pip uninstall poetry # 卸载诗歌
最后,使用poetry --version
进行测试:
如果没法使用,记得将poetry
添加到环境变量中去
二、 基础使用
1、 创建项目
poetry new poetry_demo
生成的目录结果为:
poetry-demo
├── pyproject.toml
├── README.rst
├── poetry_demo
│ └── __init__.py
└── tests
├── __init__.py
└── test_poetry_demo.py
该pyproject.toml
文件是这里最重要的。这将协调您的项目及其依赖项。现在,它看起来像这样:
[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["kun <3500515050@qq.com>"]
readme = "README.md"
packages = [{
include = "poetry_demo"}]
[tool.poetry.dependencies]
python = "^3.9"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
2、 初始化包
Poetry 可用于“初始化”预填充目录,而不是创建新项目。在目录中以交互方式创建pyproject.toml
文件pre-existing-project
cd pre-existing-project
poetry init
3、 指定依赖项
如果要向项目添加依赖项,可以在tool.poetry.dependencies
部分中指定它们。
[tool.poetry.dependencies]
pendulum = "^2.1"
如您所见,它采用包名称和版