文章目录
- 关于 poetry
- 初始化项目
- 从 0 创建项目
- 已有项目中初始化环境
- 管理依赖库
- 添加库
- 查看依赖
- 更新
- 管理环境
- 查看有哪些虚拟环境
- 删除环境
- 执行 python 脚本
- 进入环境
- manual
关于 poetry
- 官网:https://python-poetry.org
- 官方文档:https://python-poetry.org/docs/
- 视频教程:https://www.bilibili.com/video/BV1S14y1Q7pP/
对应文档:https://notes.zhengxinonly.com/environment/use-poetry.html
Poetry 差不多是 pip + venv,的结合体。可以类似 pip 用于管理第三方模块的管理,但是比 pip 的功能强大许多,同时还包含 venv 的虚拟环境管理功能。大致的功能如下:
- 管理第三方模块的安装与卸载
- 管理虚拟环境
- 管理虚拟环境的依赖
- 管理打包与发布 其中最重要的是
虚拟环境的依赖
,意识本文的重点。至于打包与发布
对于开发者用的不是很多,在这里就不介绍了。
初始化项目
从 0 创建项目
poetry new p2
结构如下
% cd p2
p2 % tree
.
├── README.md
├── p2
│ └── __init__.py
├── pyproject.toml
└── tests└── __init__.py
已有项目中初始化环境
poetry init
然后会跳出来一连串的互动对话,用于创建项目的配置文件
% poetry initThis 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
- 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
- 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 很相似