Python 虚拟环境管理库 - poetry python虚拟环境venv
简介
Poetry 是 Python 中的依赖管理和打包工具,它允许你声明项目所依赖的库,并为你管理它们。
相比于 Pipev,我觉得 poetry 更加清爽,显示更友好一些,虽然它的打包发布我们一般不使用,但是其他的虚拟环境管理也是非常方便的。
安装
pip install poetry
在控制台执行 poetry -V 命令查看版本
升级
poetry self update
poetry 配置
在使用 poetry 之前我们先进行一些设置,主要是调整一下虚拟环境的安装位置
设置项
cache-dir
Type: string
缓存目录配置,使用 poetry 安装的包源文件都会缓存到这个目录。以下是系统默认目录:
- macOS: ~/Library/Caches/pypoetry
- Windows: C:\Users<username>\AppData\Local\pypoetry\Cache
- Unix: ~/.cache/pypoetry
installer.parallel
Type: boolean
此配置会被忽略
virtualenvs.create
Type: boolean
默认为true,如果当前工程的虚拟环境不存在,就创建一个
virtualenvs.in-project
Type: boolean
- None:poetry 会在系统特定目录创建一个.venv目录,由下面的 path 参数指定
- true: poetry 会在项目根目录创建一个.venv目录,我推荐这种方式,在项目根目录创建虚拟环境,这样就算移动目录位置也不影响虚拟环境的使用
- false: poetry 将会忽略已存在的.venv目录
在 Pipev 中如果想把虚拟环境安装在当前工程根目录下,则需要通过配置环境变量 PIPENV_VENV_IN_PROJECT
virtualenvs.path
Type: string
默认是{cache-dir}/virtualenvs,虚拟环境创建的目录,如果上面的 in-project 为 true,此配置就无效
配置命令
查看所有配置
poetry config --list
查询单个配置
poetry config virtualenvs.path
添加或者更新配置
poetry config virtualenvs.in-project true
删除配置
poetry config virtualenvs.path --unset
基本使用
初始化工程
创建新工程
poetry new poetry-demo
已存在的工程
cd pre-existing-project
poetry init
创建成功后将得到如下目录:
poetry-demo-
├── pyproject.toml
├── README.rst
├── poetry_demo
│ └── __init__.py
└── tests
├── __init__.py
└── test_poetry_demo.py
pyproject.toml会是一个非常重要的文件,包含了工程的配置和依赖库信息,其初始内容如下:
[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["xingxingzaixian <beixia1989@163.com>"]
[tool.poetry.dependencies]
python = "^3.7"
[tool.poetry.dev-dependencies]
pytest = "^5.2"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
指定虚拟环境的 pypi 源
[[tool.poetry.source]]
name = "custom"
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true
pyproject.toml 文件配置介绍
tool.poetry
- name
项目名称,必选 - version
项目版本号,默认0.1.0,必选 - description
项目描述,必选 - license
开源协议 - authors
作者,这是一个列表,至少需要包含一个作者信息,必选 - maintainers
维护者 - readme
README文件 - homepage
项目首页地址
基本命令
new
# 创建新项目
poetry new project
init
# 初始化已存在的项目
poetry init project
可选项:
- --name: 名称.
- --description: 描述.
- --author: 作者.
- --python: Python 版本.
- --dependency: 要使用版本约束的包. 格式应该为 foo:1.0.0.
- --dev-dependency: 开发需求
install
poetry install
install 命令从当前项目读取 pyproject.toml 文件中的依赖性并安装它们。
如果当前目录中有poetry.lock文件,它将使用其中的确切版本,而不是解析它们。这确保使用库的每个人都将获得相同版本的依赖项。如果没有poetry.lock文件,poetry将在依赖项解析后创建一个。
update
升级安装包
add
poetry add requests # ==> pip install requests
poetry add requests@^2.20.0 # 安装大于2.20.0版本的包
poetry add "requests=2.20.0" # ==> pip install requests==2.20.0
poetry add "uvicorn[standard]" # ==> pip install uvicorn[standard]
# 从 git 仓库安装
poetry add git+https://github.com/sdispater/pendulum.git
poetry add git+ssh://git@github.com/sdispater/pendulum.git
poetry add git+https://github.com/sdispater/pendulum.git#develop
poetry add git+https://github.com/sdispater/pendulum.git#2.0.5
# 从本地文件安装
poetry add ./my-package/
poetry add ../my-package/dist/my-package-0.1.0.tar.gz
poetry add ../my-package/dist/my_package-0.1.0.whl
安装依赖库并添加到 pyproject.toml,如果没有指定版本约束,就安装最新版本
remove
poetry remove
删除已经安装的依赖包
show
# 显示已经安装的包和版本号
poetry show
# 显示非开发环境的安装包
poetry show --no-dev
# 显示依赖包的层级关系
poetry show --tree
# 显示已安装包的当前版本和最新版本
poetry show -l
# 显示可更新的包
poetry show -o
run
这个命令也是一个比较重要的命令,可以让我们不进入虚拟环境就执行虚拟环境内的命令
# 查询虚拟环境内的 Python 版本
poetry run python -V
# 执行虚拟环境内的脚本
poetry run python test.py
除了以上可以直接执行python命令外,我们还可以配置自定义的脚本命令,在 pyproject.toml 文件中配置 [tool.poetry.scripts],例如配置:
[tool.poetry.scripts]
test = "python test.py"
serve = "python run.py"
可以执行以下命令:
poetry run serve
shell
# 进入虚拟环境内
poetry shell
check
# 检查 pyproject.toml 配置是否正确
poetry check
search
# 查询包
poetry search requests
export
# 将依赖包导出为 requirements.txt 格式,导出文件名为 requirements.txt
poetry export -f requirements.txt --output requirements.txt
env
# 使用指定环境的python
poetry env use /full/path/to/python
# 如果python在环境变量中,可以使用一下方式指定
poetry env use python3.7
# 显示当前虚拟环境信息
poetry env info
# 仅显示虚拟环境的路径
poetry env info --path
# 显示当前工程的所有虚拟环境列表
poetry env list
# 显示当前工程的虚拟环境绝对路径
poetry env list --full-path
# 删除虚拟环境
poetry env remove /full/path/to/python
poetry env remove python3.7
cache
# 查看缓存列表
poetry cache list
# 清除指定的缓存
poetry cache clear <cache>
# 清除所有缓存
poetry cache clear --all
依赖包的版本关系
在 poetry 中支持类似 webpack 里面的版本关系,例如 ^2.3.0、~2.3.0,这是什么对应关系呢
要求 | 允许的版本 |
^1.2.3 | >=1.2.3 <2.0.0 |
^1.2 | >=1.2.0 <2.0.0 |
^1 | >=1.0.0 <2.0.0 |
^0.2.3 | >=0.2.3 <0.3.0 |
^0.0.3 | >=0.0.3 <0.0.4 |
^0.0 | >=0.0.0 <0.1.0 |
^0 | >=0.0.0 <1.0.0 |
~1.2.3 | >=1.2.3 <1.3.0 |
~1.2 | >=1.2.0 <1.3.0 |
~1 | >=1.0.0 <2.0.0 |