poetry是一个Python虚拟环境和依赖管理的工具,之前用pipenv,最近学习http runner时,接触了poetry。poetry和pipenv类似,另外还提供了打包和发布的功能。
python项目部署:poetry管理本地环境, 上线用docker
一、poetry安装
poetry提供多种安装方式,个人推荐从以下2种方式中选择:
方式一:(官方推荐!推荐!)
# Linux 建议使用代理
$ curl -sSL https://install.python-poetry.org | python3 -
# 安装完毕后提示下
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/.local/bin
You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.
Installing Poetry (1.3.2): Creating environment
# 在~/.bashrc中添加 export PATH=$PATH:$HOME/.poetry/bin 环境变量
# windows 等待比较久
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
# 安装完毕后结果类似Linux
# 在环境变量里添加 %USERPROFILE%\.poetry\bin
方法二:
将https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py
代码保存成本地python文件,命名为install-poetry.py
进入https://github.com/python-poetry/poetry/releases
查看最新版本
使用命令安装
python install-poetry.py --version 1.1.12
方式三:(pip)
$ pip install --user poetry
使用命令查看是否安装成功
$ poetry --version
二、卸载
Linux
curl -sSL https://install.python-poetry.org | python3 - --uninstall
curl -sSL https://install.python-poetry.org | POETRY_UNINSTALL=1 python3 -
Windows
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py - --uninstall
三、工程初始化
1、如果当前还没有创建环境,可以使用poetry环境
输入poetry new [环境名] 来创建一个项目脚手架,包括基本结构pyproject.toml文件。
$ poetry new poetry-demo
这时候,会创建一个包含如下内容的工程,
poetry-demo
├── pyproject.toml
├── README.rst
├── poetry_demo
│── __init__.py
├── tests
├── __init__.py
└── test_poetry_demo.py
2、如果新建了工程,还可以在已有工程的基础上进行创建
$ poetry init
这时候,它会让你输入包名称、版本号等信息,你可以选择输入,也可以选择按下ENTER键使用默认值,完成以后,pyproject.toml如以下格式:
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["xxxxx"]
[tool.poetry.dependencies]
python = "*"
[tool.poetry.dev-dependencies]
pytest = "^3.4" # 指定这个包只能是3.4
四、依赖包管理
安装依赖包
可以使用install命令直接解析并安装pyproject.toml的依赖包
$ poetry install
#pyproject.toml文件的配置如下:
# [tool.poetry.dependencies]
# pendulum = "^1.4"
也可以可以使用add命令来安装一款Python工具包,
$ poetry add numpy
还可以,通过添加配置参数–dev来区分不同环境下的依赖包。
详细:
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环境的依赖,一般部署时使用
安装git内网包
poetry add git+ssh@git地址
然后会在pyproject.toml里看到
net = {
git = "git@git地址", rev = "master"}
这就安装上了内网的包。
如果“git地址”的包有更新,使用到它的项目poetry update 一下就会自动更新
- 简而言是之,如果 “自己的项目”使用poetry的方式安装git上的python依赖包,那安装的python依赖包在git的master更新了,在“自己的项目”下运行 poetry update就能同步更新
更新所有锁定版本的依赖包
$ 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
查看python版本
$ poetry run python -V
更改配置
# 更改环境安装路径
$ poetry config virtualenvs.path /path/to/cache/directory/virtualenvs