Poetry 差不多是 pip + venv,的结合体。可以类似 pip 用于管理第三方模块的管理,但是比 pip 的功能强大许多,同时还包含 venv 的虚拟环境管理功能。大致的功能如下:

  1. 管理第三方模块的安装与卸载
  2. 管理虚拟环境
  3. 管理虚拟环境的依赖
  4. 管理打包与发布 其中最重要的是 虚拟环境的依赖 ,意识本文的重点。至于 打包与发布 对于开发者用的不是很多,在这里就不介绍了。

初始化项目

从 0 创建项目

 poetry new p2
% cd p2
p2 % tree
├── README.md
├── p2
│   └── __init__.py
├── pyproject.toml
└── tests
    └── __init__.py

已有项目中初始化环境

poetry init

然后会跳出来一连串的互动对话,用于创建项目的配置文件

% poetry init
This command will guide you through creating your pyproject.toml config.
Package name [t3]:  p1
Version [0.1.0]:  
Description []:  
Author [S <1625608596@qq.com>, n to skip]:  
License []:  
Compatible Python versions [^3.11]:  
Would you like to define your main dependencies interactively? (yes/no) [yes] 
You can specify a package in the following forms:
  - A single name (requests): this will search for matches on PyPI
  - A name and a constraint (requests@^2.23.0)
  - A git url (git+https://github.com/python-poetry/poetry.git)
  - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)
  - A file path (../my-package/my-package.whl)
  - A directory (../my-package/)
  - A url (https://example.com/packages/my-package-0.1.0.tar.gz)
Package to add or search for (leave blank to skip):  
Would you like to define your development dependencies interactively? (yes/no) [yes] 
Package to add or search for (leave blank to skip): 
Generated file
[tool.poetry]
name = "p1"
version = "0.1.0"
description = ""
authors = ["S <1625608596@qq.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Do you confirm generation? (yes/no) [yes] 

直接全部一路回车,然后看一下生成的 pyproject.toml 配置文件:

[tool.poetry]
name = "p1"
version = "0.1.0"
description = ""
authors = ["S <1625608596@qq.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

管理依赖库

poetry add flask 
  • 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环境的依赖,一般部署时使用

将在 [tool.poetry.dependencies] 键下面添加 flask 信息

[tool.poetry.dependencies] python = "^3.11" flask = "^3.0.3"
poetry show

按树形结构查看

poetry show -t 

更新当前环境所有依赖

poetry update

更新指定依赖包

如 requests

poetry update requests

卸载指定依赖包

poetry remove requests

查看有哪些虚拟环境

poetry env list --full-path

你可以直接删除 虚拟环境文件夹

删除指定解析器版本对应环境:

poetry env remove python2 

执行 python 脚本

poetry shell

终端命令行前缀会显示为 环境信息:(p2-py3.11)

你可以不用激活环境,因为poetry会自动检测当前虚拟环境

如果想在当前目录对应的虚拟环境中执行命令,可以使用以下命令:

poetry run <你的命令> # 例如: poetry run python flask.py

manual

% poetry --help

Description:
Lists commands.

Usage:

list [options] [–] []

Arguments:
namespace The namespace name

Options:

  • -h, --help, Display help for the given command. When no command is given display help for the list command.
  • -q, --quiet, Do not output any message.
  • -V, --version, Display this application version.
    --ansi, Force ANSI output.
    --no-ansi, Disable ANSI output.
  • -n, --no-interaction , Do not ask any interactive question.
    --no-plugins, Disables plugins.
    --no-cache, Disables Poetry source caches.
  • -C, --directory=DIRECTORY, The working directory for the Poetry command (defaults to the current working directory).
  • -v|vv|vvv, --verbose, Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.

Help:
The list command lists all commands:

poetry list

You can also display the commands for a specific namespace:

poetry list test

Available commands:

  • about Shows information about Poetry.
  • add Adds a new dependency to pyproject.toml and installs it.
  • build Builds a package, as a tarball and a wheel by default.
  • check Validates the content of the pyproject.toml file and its consistency with the poetry.lock file.
  • config Manages configuration settings.
  • export Exports the lock file to alternative formats.
  • help Displays help for a command.
  • init Creates a basic pyproject.toml file in the current directory.
  • install Installs the project dependencies.
  • list Lists commands.
  • lock Locks the project dependencies.
  • new Creates a new Python project at
  • publish Publishes a package to a remote repository.
  • remove Removes a package from the project dependencies.
  • run Runs a command in the appropriate environment.
  • search Searches for packages on remote repositories.
  • shell Spawns a shell within the virtual environment.
  • show Shows information about packages.
  • update Update the dependencies as according to the pyproject.toml file.
  • version Shows the version of the project or bumps it when a valid bump rule is provided.

cache

  • cache clear Clears a Poetry cache by name.
  • cache list List Poetry’s caches.

debug

  • debug info Shows debug information.
  • debug resolve Debugs dependency resolution.
  • env info Displays information about the current environment.
  • env list Lists all virtualenvs associated with the current project.
  • env remove Remove virtual environments associated with the project.
  • env use Activates or creates a new virtualenv for the current project.
  • self add Add additional packages to Poetry’s runtime environment.
  • self install Install locked packages (incl. addons) required by this Poetry installation.
  • self lock Lock the Poetry installation’s system requirements.
  • self remove Remove additional packages from Poetry’s runtime environment.
  • self show Show packages from Poetry’s runtime environment.
  • self show plugins Shows information about the currently installed plugins.
  • self update Updates Poetry to the latest version.

source

  • source add Add source configuration for project.
  • source remove Remove source configured for the project.
  • source show Show information about sources configured for the project.

伊织 2024-05-14(二)
个人觉得这个体验和 rust - cargo 很相似

poetry 核心之一:使项目环境隔离,意味着始终和本地全局 Python 环境隔离 poetry 首先会检查当前项目是否在虚拟环境中运行:如果是将直接使用它,而不创建新的;如果不是,poetry 将使用它已创建的或创建一个全新的虚拟环境 默认情况下,poetry 将尝试使用当前激活的 Python 版本为当前项目创建虚拟环境 如果当前 Python 版本可能和项目的 Python ... Poetry 是一个包管理和打包的工具。 在 Python 中,对于初学者来说,打包系统和依赖管理是非常复杂和难懂的。即使对于经验丰富的开发者,一个项目总是要同时创建多个文件: setup.py ,requirements.txt,setup.cfg , MANIFEST.in ,还有最新的 Pipfile,十分繁琐。因此, poetry 将所有的配置都放置在一个 toml 文件(pyproject.toml)中,这些配置包括:依赖管理、构建、打包、发布。 Poetry 的灵感来自于其他语言的一些工具 这会将`virtualenvs.in-project`设置为`true`,并将其写入`$POETRY_HOME/config.toml`文件中。 2. 直接编辑`$POETRY_HOME/config.toml`文件,设置相应的环境变量。例如: [env] PYTHONPATH = "/path/to/my/python/modules" 这会将`PYTHONPATH`设置为`/path/to/my/python/modules`。 请注意,`$POETRY_HOME`是一个环境变量,它指向Poetry的用户目录。默认情况下,它位于`~/.poetry`目录下。你可以通过`echo $POETRY_HOME`命令来查看`$POETRY_HOME`的值。