poetry—Python环境管理工具

Python包管理之poetry的使用

poetry是一个Python虚拟环境和依赖管理的工具,之前用pipenv,最近学习httprunner时,接触了poetry。poetry和pipenv类似,另外还提供了打包和发布的功能。
官方文档:https://python-poetry.org/docs/

python项目部署:poetry管理本地环境,上线用docker

一、poetry安装

poetry提供多种安装方式,个人推荐从以下2种方式中选择:

方式一:(官方推荐推荐)

# Linux
$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
# 安装完毕后提示下
It will add the `poetry` command to Poetry's bin directory, located at:

$HOME/.poetry/bin

This path will then be added to your `PATH` environment variable by
modifying the profile file located at:

$HOME/.profile

You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.

Installing version: 1.1.6
  - Downloading poetry-1.1.6-linux.tar.gz (72.33MB)

Poetry (1.1.6) is installed now. Great!

To get started you need Poetry's bin directory ($HOME/.poetry/bin) in your `PATH`
environment variable. Next time you log in this will be done
automatically.

To configure your current shell run `source $HOME/.poetry/env`
# 在终端使用命令 source $HOME/.poetry/env 添加环境变量
# 重启

# windows 等待比较久
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
# 安装完毕后结果类似Linux
# 在环境变量里添加 %USERPROFILE%\.poetry\bin

方式二:(pip)

$ pip install --user poetry

使用命令查看是否安装成功

$ poetry --version

二、工程初始化

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

更新所有锁定版本的依赖包

$ 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

你可能感兴趣的:(环境依赖,python)