跳到主要内容

Python 的包管理器 poetry

poetry 是什么

poetry 是一个 Python 虚拟环境和依赖管理的工具, poetry 和 pipenv 类似,另外还提供了打包和发布的功能

  • 依赖管理: Poetry 会处理依赖项之间的兼容性,确保一致的构建环境。
  • 简化打包和发布: 使用简单的命令来打包和发布项目。
  • 锁文件: 使用 poetry.lock 文件确保开发者和部署环境的一致性。

Linux

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

Windows

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -

它被安装到了以下路径

%USERPROFILE%\AppData\Roaming\pypoetry

# 设置环境变量
set PATH=%PATH%;%USERPROFILE%\AppData\Roaming\pypoetry\venv\Scripts

然后设置环境变量

在 windows 资源管理器中输入: %USERPROFILE%\.poetry\bin

基本使用

Poetry 是一个现代的 Python 依赖管理和打包工具。它旨在简化包的依赖管理和发布过程。下面是一些 Poetry 的常用方式:

使用 poetry new 创建一个新的 Python 项目:

poetry new my-project

这将创建一个包含基本目录结构和配置文件(pyproject.toml)的新项目。

虚拟环境

Poetry 会自动为你的项目创建一个虚拟环境。你可以使用以下命令来激活虚拟环境:

poetry shell

这会启动一个新的 shell 并激活虚拟环境。

查看 Poetry 创建的虚拟环境的路径,可以使用以下命令:

poetry env info --path

依赖管理

使用 poetry add 将依赖项添加到项目中:

poetry add requests

这将安装最新版本的 requests 并将其添加到 pyproject.tomlpoetry.lock 文件中,后者记录了精确的依赖版本,以确保项目的可重现性。

要安装项目的所有依赖项,请运行:

poetry install

这会读取 pyproject.toml 文件,并安装所有列出的依赖。

要更新特定依赖项,使用:

poetry update requests

要更新所有依赖项,运行:

poetry update

如果要安装本地包,可以使用 poetry add 命令,指定本地包的路径:

poetry add /path/to/local-package

打包和发布

使用 poetry build 命令来打包你的项目:

poetry build

这将在 dist 目录下生成 wheel 和 source 包。

发布到 PyPI(或其他仓库),首先配置 PyPI 凭据,然后使用:

poetry publish

运行脚本

使用 poetry run 来在 Poetry 创建的虚拟环境中运行脚本:

poetry run python script.py

poetry 使用 pypi 镜像

Poetry 官方文档中没有直接提供在安装依赖 (poetry addpoetry install) 时直接指定镜像源的命令

  1. 全局配置:你可以通过 Poetry 的全局配置来设置默认的 PyPI 镜像,这样所有使用 Poetry 的项目都会默认使用这个镜像。打开终端或命令行界面,运行以下命令:
poetry config repositories.<repository-name> <mirror-url>
poetry config http-basic.<repository-name> <username> <password>

这里的 <repository-name> 是你给镜像源起的名字, <mirror-url> 是镜像的 URL。如果镜像不需要认证,可以省略 http-basic 的配置。例如,使用中国科技大学的 PyPI 镜像:

poetry config repositories.ustc https://mirrors.ustc.edu.cn/pypi/web/simple
  1. 项目配置:如果你想要为特定的项目设置 PyPI 镜像,可以在项目目录中运行上述命令,加上 --local 参数,这会修改项目目录下的 pyproject.toml 文件而非全局配置。

通过 pyproject.toml 文件

除了使用命令行,你也可以直接编辑项目的 pyproject.toml 文件来指定 PyPI 镜像。在文件中添加或修改 tool.poetry.source 部分,例如:

[[tool.poetry.source]]
name = "ustc"
url = "https://mirrors.ustc.edu.cn/pypi/web/simple"
default = true

或者

[[tool.poetry.source]]
name = "mirror"
url = "https://mirror.baidu.com/pypi/simple"
default = true

这样配置后,Poetry 会使用指定的镜像源来安装依赖。 用镜像后遇到包版本找不到或其他问题,检查镜像是否最新,或尝试切换到其他镜像。

通过上述方法,你可以方便地在使用 Poetry 进行项目管理时使用 PyPI 的镜像,从而提高包下载速度,解决网络访问问题。

在 vscode 中使用 poetry

在 vscode 中使用 poetry 有两种方式,一种是使用插件,一种是使用 vscode 自带的功能

  1. 打开 VS Code 的命令面板(Ctrl+Shift+P 或 Cmd+Shift+P)。
  2. 输入并选择 “Python: Select Interpreter”。
  3. 从列表中选择Poetry创建的虚拟环境。如果你的项目虚拟环境没有出现在列表中,你可能需要先在项目目录下通过命令行运行 poetry install 来创建虚拟环境。

编写 vscode 的 .vscode/lanch.json 文件

{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}", // 当前打开的文件
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}/lazy_rap", // 设置工作目录为源代码目录
"env": {
"PYTHONPATH": "${workspaceFolder}/lazy_rap" // 确保 Python 可以导入项目模块
}
}
]
}

References