Python依賴管理及打包工具Poetry使用規(guī)范
啥是依賴規(guī)范
可以以各種形式指定項目的依賴項,取決于依賴項的類型以及安裝項目可能需要的可選約束
版本約束
^ 約束
編寫規(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ù)字為主版本號,比如:^2.13.0,可以取 2.14.0,但不能取 3.0.0,因為主版本號已經(jīng)變了
- 如果左一的數(shù)字為 0,則以左二的數(shù)字為主版本號,比如:^0.1.0 可以取 0.1.1、0.1.19,但不能取 0.2.0,因為主版本號已經(jīng)變了
~ 約束
編寫規(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 |
和上面的 ^ 差不多,不過這個是次要版本,以第二個數(shù)字為基準(zhǔn)
* 約束
有點像萬能匹配符,寫在哪里都可以
編寫規(guī)范 | 允許的版本范圍 |
---|---|
* | >=0.0.0 |
1.* | >=1.0.0 <2.0.0 |
1.2.* | >=1.2.0 <1.3.0 |
比較符
就常規(guī)的>、< 符號了
>= 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" }
默認(rèn)會拉 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 依賴
如果依賴遠(yuǎn)程倉庫的文件,可以用 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 限制依賴項
指定僅應(yīng)該以特定 Python 版本安裝依賴項
[tool.poetry.dependencies] pathlib2 = { version = "^2.2", python = "~2.7" }
[tool.poetry.dependencies] pathlib2 = { version = "^2.2", python = "~2.7 || ^3.2" }
多個限制
假設(shè)依賴包
版本小于等于 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"} ]
使用環(huán)境限制
感覺比較少用,暫時不展開詳解
[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
擴展依賴規(guī)范語法
當(dāng)某個依賴項需要添加很多屬性的時候,可讀性就很差,如下
[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依賴規(guī)范的詳細(xì)內(nèi)容,更多關(guān)于Python工具poetry依賴規(guī)范的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 專題九 Mysql數(shù)據(jù)庫編程基礎(chǔ)知識
在Python網(wǎng)絡(luò)爬蟲中,通常是通過TXT純文本方式存儲,其實也是可以存儲在數(shù)據(jù)庫中的;同時在WAMP(Windows、Apache、MySQL、PHP或Python)開發(fā)網(wǎng)站中,也可以通過Python構(gòu)建網(wǎng)頁的,所以這篇文章主要講述Python調(diào)用MySQL數(shù)據(jù)庫相關(guān)編程知識2017-03-03Python實現(xiàn)獲取前100組勾股數(shù)的方法示例
這篇文章主要介紹了Python實現(xiàn)獲取前100組勾股數(shù)的方法,涉及Python數(shù)值計算與判斷相關(guān)操作技巧,需要的朋友可以參考下2018-05-05淺談matplotlib.pyplot與axes的關(guān)系
這篇文章主要介紹了淺談matplotlib.pyplot與axes的關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03flask框架自定義過濾器示例【markdown文件讀取和展示功能】
這篇文章主要介紹了flask框架自定義過濾器,結(jié)合實例形式分析了flask基于自定義過濾器實現(xiàn)markdown文件讀取和展示功能相關(guān)操作技巧,需要的朋友可以參考下2019-11-11