Skip to main content

虚拟环境的作用

虚拟环境变量在打包中最大的作用是减少最终打包文件的体积

常用的虚拟环境库

poetry

官方首页

image-20220828125457014

使用流程

  • 安装poetry
  • 全局配置
  • 创建项目 - init
  • 添加依赖 - add
  • 安装依赖 - install

安装(系统中python已添加到环境变量)

  • widnow
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | D:\CPS\python\Python_3.10.2_x64_green\python.exe

下载完成后,用户文件夹内会新增两个poetry的执行文件

# {user_name}/.poetry
|-- bin/ # 「bin」
| |-- poetry.bat #
| `-- poetry # 修改这个文件里面的第一行 python的路径
|-- lib/ # 「lib」
  • linux
curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | <python>
  • pip
pip install --user poetry

安装(绿色安装)

  • window
# setp1: 下载官方的安装脚本,保存为pypoetry-install.py
https://install.python-poetry.org/

# setp2: 通过绝对路径的方式调用绿色版的python执行该脚本
# 注意指定pip的安装源
.\python.exe ..\pypoetry-install.py

# setp3: 如何源正确,基本能看到以下提示信息

image-20220901150428364

接下来根据提示将安装好的poetry添加到系统环境变量

image-20220901150517575

# 测试
poetry --version

安装(pip)- 暂时失败

  • window
# setp1: 指定一个虚拟环境存放目录
python3 -m venv $VENV_PATH
.\python.exe -m venv w:\cps\python\poetry\venv_path

# 进入刚刚新建的虚拟环境
# 注意这里用的是 .ps1,如果使用cmd作为终端,可以用activate.bat
W:\cps\python\poetry\venv_path\Scripts\Activate.ps1
.\python.exe -m pip install -U pip setuptools

image-20220901144352246

如图所示,出现(venv_path)既说明进入虚拟环境成功,后面接着更新pip和安装poetry

.\python.exe -m pip install poetry

当要卸载 Poetry时,只需删除整个$VENV_PATH目录

更新

poetry self update

poetry self update --preview

poetry self update 0.8.0

源设置

[[tool.poetry.source]]
name = "tsinghua" # 源名称
url = "https://pypi.tuna.tsinghua.edu.cn/simple/" # 源地址

创建项目

  • 新项目
# 在当前文件夹中创建项目,同时创建src文件夹
poetry new . --src
  • 已存在的项目加入peotry
poeyry init <project_name> --src

常用指令

语法:poetry <command>

名称功能
new创建一个项目脚手架,包含基本结构、pyproject.toml 文件
init基于已有的项目代码创建 pyproject.toml 文件,支持交互式填写
install安装依赖库
update更新依赖库
add添加依赖库
remove移除依赖库
show查看具体依赖库信息,支持显示树形依赖链
build构建 tar.gz 或 wheel 包
publish发布到 PyPI
run运行脚本和代码
env虚拟环境管理,对虚拟环境进行各种操作

install指令会读取pyproject.tomlpoetry.lock,并安装里面列出的依赖,对poetry.lock的处理方式是这样的:如果有这个文件,就安装里面指定的版本的包,如果没有,那就从pyproject.toml中计算依赖,并安装满足条件的最新版本的包。额外的,如果不想安装开发版本依赖,需要加上--no-dev参数

虚拟环境

官方文档

常用指令

poetry env info # 查看当前虚拟环境情况
poetry env list # 列出当前所有虚拟环境
poetry env list --full-path # 显示虚拟环境绝对路径
poetry env remove # 删除指定的虚拟环境
poetry env remove python3 # 执行删除虚拟环境时,需要指定对应的解析器版本

配置文件

官方所有字段解析

配置文件

可以通过POETRY_CONFIG_DIR环境变量来配置

  • macOS: ~/Library/Application Support/pypoetry/config.toml
  • Windows: C:\Users\<username>\AppData\Roaming\pypoetry\config.toml
# windows下快速打开配置文件目录
start $ENV:AppData\pypoetry\

生产环境常用配置

# config.toml 文件
cache-dir = "W:\\CPS\\python\\Poetry\\cache"

[virtualenvs]
create = true # 始终使用虚拟环境
path = "{dir}" # 始终指定一个目录存放虚拟环境
in-project = ture # 在项目目录创建虚拟环境
always-copy = false # 确保虚拟环境的文件不是链接,这样可能会让整个项目非常大
prefer-active-python = true # 是否始终使用安装时的python版本

[virtualenvs.options]
no-pip = true # 生产环境中的虚拟环境,不生成pip
no-setuptools = true # 生产环境中的虚拟环境,不生成setup
system-site-packages = false # 是否通过system来访问全局的site-packages
cache-dir = "W:\\CPS\\python\\Poetry\\cache"

[virtualenvs]
create = true
prefer-active-python = true
always-copy = false
in-project = ture

[virtualenvs.options]
no-pip = true
no-setuptools = true
system-site-packages = false

常用指令

# 列出所有配置
poetry config --list
poetry config cache-dir d:\python\cache-dir

# 列出某个配置
poetry config virtualenvs.path

# 全局配置
poetry config virtualenvs.create false

# 项目配置
poetry config virtualenvs.create false --local


# 某个字段恢复默认配置
poetry config virtualenvs.path --unset

项目配置

文件路径:{项目目录}/pyproject.toml

[tool.poetry]
name = "my-package"
version = "0.1.0"
description = "The description of the package"

license = "MIT"

authors = [
"Sébastien Eustace <sebastien@eustace.io>"
]

readme = "README.md"
repository = "https://github.com/python-poetry/poetry"
homepage = "https://python-poetry.org"
keywords = ["packaging", "poetry"]

# 生产依赖
[tool.poetry.dependencies]
python = "^3.8" # Compatible python versions must be declared here
aiohttp = "^3.8.1"

# Dependencies with extras
requests = { version = "^2.28", extras = [ "security" ] }
# Python specific dependencies with prereleases allowed
tomli = { version = "^2.0.1", python = "<3.11", allow-prereleases = true }
# Git dependencies
cleo = { git = "https://github.com/python-poetry/cleo.git", branch = "master" }

# Optional dependencies (extras)
pendulum = { version = "^2.1.2", optional = true }

# 开发依赖
[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
pytest-cov = "^3.0"

[tool.poetry.scripts]
my-script = "my_package:main"

[[tool.poetry.source]]
name = "tsinghua" # 源名称
url = "https://pypi.tuna.tsinghua.edu.cn/simple/" # 源地址

环境变量

变量名称说明
POETRY_CONFIG_DIR配置文件config.toml的存放目录
POETRY_DATA_DIR所有数据文件的存放目录
POETRY_CACHE_DIR所有缓存的目录

poetry 支持通过环境变量的形式配置config.toml

# 所有变量名采用 POETRY_ 开头
# 其余所有字段均用大写

export POETRY_VIRTUALENVS_PATH=/path/to/virtualenvs/directory

export POETRY_HTTP_BASIC_MY_REPOSITORY_PASSWORD=secret