广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python依赖管理及打包工具Poetry使用规范
  • 816
分享到

Python依赖管理及打包工具Poetry使用规范

2024-04-02 19:04:59 816人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录啥是依赖规范版本约束^ 约束~ 约束* 约束比较符git 依赖路径依赖url 依赖python 限制依赖项多个限制使用环境限制扩展依赖规范语法啥是依赖规范 可以以各种形式指定项目

啥是依赖规范

可以以各种形式指定项目的依赖项,取决于依赖项的类型以及安装项目可能需要的可选约束

版本约束

^ 约束

编写规范 允许的版本范围
^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'"

依赖项的约束完全一样,只不过变成一行一个约束属性,可读性更强

以上就是Python依赖管理及打包工具Poetry依赖规范的详细内容,更多关于Python工具poetry依赖规范的资料请关注编程网其它相关文章!

--结束END--

本文标题: Python依赖管理及打包工具Poetry使用规范

本文链接: https://www.lsjlt.com/news/137121.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python依赖管理及打包工具Poetry使用规范
    目录啥是依赖规范版本约束^ 约束~ 约束* 约束比较符git 依赖路径依赖url 依赖Python 限制依赖项多个限制使用环境限制扩展依赖规范语法啥是依赖规范 可以以各种形式指定项目...
    99+
    2022-11-12
  • Python 依赖管理及打包三方库 Poetry
    一、背景 最近python的ui自动化工程中需要调研和选取一个python第三方包的依赖管理工具,近期poetry比较火,就选择了这个新的工具,以下来介绍Python环境管理Poetry的使用。 介绍: Poetry 是 Python ...
    99+
    2023-10-08
    windows python 开发语言
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作