Poetry ---- Python Package Manager and Build Tool
文章目录
安装
win10
- 不要使用 pipx 安装
- 使用官方 install-poetry.py
换源 mirror —- 镜像管理
换源
- 当前项目修改
使用命令 poetry source add
| |
全局换源
1 2# 不推荐的方法,硬hack, 验证无效 poetry config repositories.pypi https://pypi.doubanio.com/simple/参考:
安装依赖使用指定源
poetry add --source douban <yourpackage>
说明:这样可以对不同的包使用不同的源
包管理 和 虚拟环境
- 类似 pipenv
参考:poetry dependency-specification
@^2.0.5
- 仅当前小版本内升级, 2.0.5, 2.0.6, ….
@latest
- 最新版
eg:
- poetry add pendulum@^2.0.5
- poetry add "pendulum>=2.0.5"
- poetry add pendulum@latest
- 安装 pyproject.toml 声明的 library
依赖管理
安装所有依赖
poetry install
添加依赖
poetry add <package>
- –dev (-D)
支持格式
- PYPI 在线源
git
- git+https:
poetry add git+https://github.com/sdispater/pendulum.git#develop - git+ssh:
poetry add git+ssh://git@github.com/sdispater/pendulum.git
- git+https:
本地文件
- 压缩包:
poetry add ../my-package/dist/my-package-0.1.0.tar.gz - wheel 格式文件:
poetry add ../my-package/dist/my_package-0.1.0.whl
- 压缩包:
- 文件夹:
poetry add ./path/to/my-package/
删除依赖
- poetry remove <package>
依赖查看
- poetry show
搜索包
poetry search
- pipenv 没有这个功能
依赖格式转换
- poetry export -f requirements.txt –output ./my/path/requirements.txt
依赖声明格式
参考:
- ^3.10 means >=3.10,<4 - but ^3.10.0 means >=3.10,<3.11
虚拟环境配置
详情
poetry config --list- virtualenv 路径
指定 python 版本
- poetry env use python3.8
注意
- 类似 pipenv, virtualenv 命令,需要是已经安装的本地 python
- poetry shell 长久激活
- poetry run 单次启动
特点
- 没有指定 pyhon env 之前
- 使用外部的 虚拟环境,比如当前使用的 conda py38(当前激活 的)
poetry add|remove <package>会直接安装作用到 py38 中
项目管理
新建项目
- poetry new <project_name>
已有项目初始化
- poetry init <project_name>
项目结构生成
生成 pyproject.toml 文件
- 依赖、发布、项目元数据配置
- tests 文件夹
- soure 存放文件夹(project_folder/project_folder)
配置文件
- pyproject.toml
- poetry.lock
打包 和 发布
poetry build
- 打包成两种格式 sdist 和 编译的包 wheel
- poetry publish
bash 自动补全 completion
poetry completions bash >|sudo tee /etc/bash_completion.d/poetry.bash-completion
虚拟环境设置
- poetry env
创建
- poetry env use <python> | <3.9> | </path/to/python>
注意:
使用 python | python3.9 | 3.9 前提
- 要求给定的 python 命令在 PATH 变量中
直接使用 python 的危险
- PATH 变量中存在多个命令,切换变量可能导致 python 命令不是想要的版本
- 推荐:尽量使用具体版本号
列举
- poetry env list
删除
- poetry env remove <3.9> | <full-name, eg: test-O3eWbxRl-py3.7> | </path/to/python>
- 删除所有:
poetry env remove --all 删除单个:
poetry env remove /path/to/python- 获取 python 路径方法:
poetry env info
- 获取 python 路径方法:
详情
- poetry env info [-p]
- 当前 poetry env use 关联的 外部 python 和 制作的 virtualenv 信息
- 如果没有关联,显示外部信息
注意
- windows 系统中,可能创建 virtualenv 失败,一直都是外部信息
配置 命令
poetry config参数
–local
- 在项目根目录生成 poetry.toml 文件,记录项目本地 poetry 配置
自定义 打包和编译
- 参考:python-poetry/poetry#2740 Stabilize 'build.py'
- 通过禁用自动生成 setup.py 文件,自定义 build.py 文件实现
例子
1 2 3 4 5 6 7[tool.poetry.build] script = "build.py" generate-setup-file = false # 或者下面这种 [tool.poetry] build = "build.py"
与 tox 联用问题
目的
- tox 把依赖管理全部交给 poetry
参考
配置文件
pyproject.toml 设置
- 无需修改
tox.ini
1 2 3 4 5 6 7 8 9[tox] isolated_build = true envlist = py27, py36 [testenv] whitelist_externals = poetry commands = poetry install --no-root -v poetry run pytest tests/
出现问题
poetry install 不能安装到 tox 虚拟环境
解决办法
- 删除 poetry env, 即可
poetry 安装特别慢
- 无需处理,第二次使用 tox, 即可快速完成
导入 requirement.txt 依赖
参考:
例子:
xargs
1cat requirements.txt|xargs poetry add
文章作者
上次更新 2025-02-24 (77fafc9)