Python依赖管理及打包工具Poetry使用规范
作者:小菠萝测试笔记 时间:2021-06-02 13:21:43
啥是依赖规范
可以以各种形式指定项目的依赖项,取决于依赖项的类型以及安装项目可能需要的可选约束
版本约束
^ 约束
编写规范 | 允许的版本范围 |
---|---|
^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 |
当最左边的数字为非 0,则以左一数字为主版本号,比如:^2.13.0,可以取 2.14.0,但不能取 3.0.0,因为主版本号已经变了
如果左一的数字为 0,则以左二的数字为主版本号,比如:^0.1.0 可以取 0.1.1、0.1.19,但不能取 0.2.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 |
和上面的 ^ 差不多,不过这个是次要版本,以第二个数字为基准
* 约束
有点像万能匹配符,写在哪里都可以
编写规范 | 允许的版本范围 |
---|---|
* | >=0.0.0 |
1.* | >=1.0.0 <2.0.0 |
1.2.* | >=1.2.0 <1.3.0 |
比较符
就常规的>、< 符号了
>= 1.2.0
> 1
< 2
!= 1.2.3
确定的版本号或范围
>= 1.2,< 1.5
git 依赖
可以指定依赖项的 git 仓库地址
[tool.poetry.dependencies]
requests = { git = "https://github.com/requests/requests.git" }
默认会拉 git 仓库的 master 分支
也可以指定 branch、commit hash、tag
[tool.poetry.dependencies]
# Get the latest revision on the branch named "next"
requests = { git = "https://github.com/kennethreitz/requests.git", branch = "next" }
# Get a revision by its commit hash
flask = { git = "https://github.com/pallets/flask.git", rev = "38eb5d3b" }
# Get a revision by its tag
numpy = { git = "https://github.com/numpy/numpy.git", tag = "v0.13.2" }
路径依赖
如果依赖项位于本地目录,可以用 path
[tool.poetry.dependencies]
# directory
my-package = { path = "../my-package/", develop = false }
# file
my-package = { path = "../my-package/dist/my-package-0.1.0.tar.gz" }
url 依赖
如果依赖远程仓库的文件,可以用 url
[tool.poetry.dependencies]
# directory
my-package = { url = "https://example.com/my-package-0.1.0.tar.gz" }
可以通过 poetry add 来添加 url
poetry add https://example.com/my-package-0.1.0.tar.gz
Python 限制依赖项
指定仅应该以特定 Python 版本安装依赖项
[tool.poetry.dependencies]
pathlib2 = { version = "^2.2", python = "~2.7" }
[tool.poetry.dependencies]
pathlib2 = { version = "^2.2", python = "~2.7 || ^3.2" }
多个限制
假设依赖包
版本小于等于 1.9 的时候,只能和 Python 2.7 到 Python 2.9 版本兼容
版本大于 2.0 的时候,只能和 Python 3.4 + 版本兼容
[tool.poetry.dependencies]
foo = [
{version = "<=1.9", python = "^2.7"},
{version = "^2.0", python = "^3.4"}
]
使用环境限制
感觉比较少用,暂时不展开详解
[tool.poetry.dependencies]
pathlib2 = { version = "^2.2", markers = "python_version ~= '2.7' or sys_platform == 'win32'" }
markers 官方文档:https://www.python.org/dev/peps/pep-0508/#environment-markers
扩展依赖规范语法
当某个依赖项需要添加很多属性的时候,可读性就很差,如下
[tool.poetry.dev-dependencies]
black = {version = "19.10b0", allow-prereleases = true, python = "^3.6", markers = "platform_python_implementation == 'CPython'"}
使用新的语法格式
[tool.poetry.dev-dependencies.black]
version = "19.10b0"
allow-prereleases = true
python = "^3.6"
markers = "platform_python_implementation == 'CPython'"
依赖项的约束完全一样,只不过变成一行一个约束属性,可读性更强
来源:https://blog.csdn.net/qq_33801641/article/details/120320778
标签:Python,Poetry,依赖,规范
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用torchtext导入NLP数据集的操作
2023-08-19 16:07:10
![](https://img.aspxhome.com/file/2023/6/85276_0s.jpg)
详解python内置模块urllib
2022-11-18 21:34:10
![](https://img.aspxhome.com/file/2023/7/77627_0s.png)
OpenCV图像缩放resize各种插值方式的比较实现
2023-03-18 13:31:45
![](https://img.aspxhome.com/file/2023/0/92070_0s.png)
如何在Python中引用其他模块
2022-09-26 23:30:21
![](https://img.aspxhome.com/file/2023/2/69492_0s.png)
asp用err.raise自定义错误信息
2007-09-12 19:38:00
ASP中 ADODB.Stream 语法速查
2008-01-27 15:23:00
js实现用div层模拟的小窗口
2007-12-02 15:01:00
![](https://img.aspxhome.com/file/UploadPic/200712/2/2007122151314277s.gif)
python如何读取和存储dict()与.json格式文件
2021-12-07 16:13:36
![](https://img.aspxhome.com/file/2023/6/90326_0s.png)
对numpy中向量式三目运算符详解
2023-06-28 02:14:55
系统默认的MySQL用户名消失的解决方法
2008-12-02 14:26:00
ASP与MySQL的连接[图文教程]
2010-03-14 11:21:00
![](https://img.aspxhome.com/file/UploadPic/20093/9/200939182427439s.png)
DW实现鼠标滑过切换图片
2008-02-03 18:49:00
![](https://img.aspxhome.com/file/UploadPic/20082/3/200823185431657s.jpg)
从一个项目中来看三层架构
2008-08-06 12:50:00
Python Opencv轮廓常用操作代码实例解析
2023-01-03 08:46:59
![](https://img.aspxhome.com/file/2023/6/75306_0s.png)
如何检测用户第一次访问我的网站并显示友好信息?
2009-11-25 20:33:00
手写一个python迭代器过程详解
2021-06-29 07:45:23
Mootools 1.2教程(8)——输入过滤第一部分(数字)
2008-11-27 13:01:00
Python OpenCV实现图像傅里叶变换
2022-06-19 04:43:55
![](https://img.aspxhome.com/file/2023/4/92314_0s.png)
Python读取csv文件实例解析
2023-01-21 07:47:32
![](https://img.aspxhome.com/file/2023/8/68408_0s.png)
MySQL Basis 常用命令
2010-11-11 11:59:00