Python依賴(lài)管理及打包工具Poetry使用規(guī)范
啥是依賴(lài)規(guī)范
可以以各種形式指定項(xiàng)目的依賴(lài)項(xiàng),取決于依賴(lài)項(xiàng)的類(lèi)型以及安裝項(xiàng)目可能需要的可選約束
版本約束
^ 約束
編寫(xiě)規(guī)范 | 允許的版本范圍 |
---|---|
^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 |
- 當(dāng)最左邊的數(shù)字為非 0,則以左一數(shù)字為主版本號(hào),比如:^2.13.0,可以取 2.14.0,但不能取 3.0.0,因?yàn)橹靼姹咎?hào)已經(jīng)變了
- 如果左一的數(shù)字為 0,則以左二的數(shù)字為主版本號(hào),比如:^0.1.0 可以取 0.1.1、0.1.19,但不能取 0.2.0,因?yàn)橹靼姹咎?hào)已經(jīng)變了
~ 約束
編寫(xiě)規(guī)范 | 允許的版本范圍 |
---|---|
~1.2.3 | >=1.2.3 <1.3.0 |
~1.2 | >=1.2.0 <1.3.0 |
~1 | >=1.0.0 <2.0.0 |
和上面的 ^ 差不多,不過(guò)這個(gè)是次要版本,以第二個(gè)數(shù)字為基準(zhǔn)
* 約束
有點(diǎn)像萬(wàn)能匹配符,寫(xiě)在哪里都可以
編寫(xiě)規(guī)范 | 允許的版本范圍 |
---|---|
* | >=0.0.0 |
1.* | >=1.0.0 <2.0.0 |
1.2.* | >=1.2.0 <1.3.0 |
比較符
就常規(guī)的>、< 符號(hào)了
>= 1.2.0 > 1 < 2 != 1.2.3
確定的版本號(hào)或范圍
>= 1.2,< 1.5
git 依賴(lài)
可以指定依賴(lài)項(xiàng)的 git 倉(cāng)庫(kù)地址
[tool.poetry.dependencies] requests = { git = "https://github.com/requests/requests.git" }
默認(rèn)會(huì)拉 git 倉(cāng)庫(kù)的 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" }
路徑依賴(lài)
如果依賴(lài)項(xiàng)位于本地目錄,可以用 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 依賴(lài)
如果依賴(lài)遠(yuǎn)程倉(cāng)庫(kù)的文件,可以用 url
[tool.poetry.dependencies] # directory my-package = { url = "https://example.com/my-package-0.1.0.tar.gz" }
可以通過(guò) poetry add 來(lái)添加 url
poetry add https://example.com/my-package-0.1.0.tar.gz
Python 限制依賴(lài)項(xiàng)
指定僅應(yīng)該以特定 Python 版本安裝依賴(lài)項(xiàng)
[tool.poetry.dependencies] pathlib2 = { version = "^2.2", python = "~2.7" }
[tool.poetry.dependencies] pathlib2 = { version = "^2.2", python = "~2.7 || ^3.2" }
多個(gè)限制
假設(shè)依賴(lài)包
版本小于等于 1.9 的時(shí)候,只能和 Python 2.7 到 Python 2.9 版本兼容
版本大于 2.0 的時(shí)候,只能和 Python 3.4 + 版本兼容
[tool.poetry.dependencies] foo = [ {version = "<=1.9", python = "^2.7"}, {version = "^2.0", python = "^3.4"} ]
使用環(huán)境限制
感覺(jué)比較少用,暫時(shí)不展開(kāi)詳解
[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
擴(kuò)展依賴(lài)規(guī)范語(yǔ)法
當(dāng)某個(gè)依賴(lài)項(xiàng)需要添加很多屬性的時(shí)候,可讀性就很差,如下
[tool.poetry.dev-dependencies] black = {version = "19.10b0", allow-prereleases = true, python = "^3.6", markers = "platform_python_implementation == 'CPython'"}
使用新的語(yǔ)法格式
[tool.poetry.dev-dependencies.black] version = "19.10b0" allow-prereleases = true python = "^3.6" markers = "platform_python_implementation == 'CPython'"
依賴(lài)項(xiàng)的約束完全一樣,只不過(guò)變成一行一個(gè)約束屬性,可讀性更強(qiáng)
以上就是Python依賴(lài)管理及打包工具Poetry依賴(lài)規(guī)范的詳細(xì)內(nèi)容,更多關(guān)于Python工具poetry依賴(lài)規(guī)范的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。