天天看點

Python依賴管理及打包工具Poetry使用規範

啥是依賴規範

可以以各種形式指定項目的依賴項,取決于依賴項的類型以及安裝項目可能需要的可選限制

版本限制

^ 限制

編寫規範 允許的版本範圍
^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

和上面的 ^ 差不多,不過這個是次要版本,以第二個數字為基準。最後,如果你的時間不是很緊張,并且又想快速的提高,最重要的是不怕吃苦,建議你可以價位@762459510 ,那個真的很不錯,很多人進步都很快,需要你不怕吃苦哦!大家可以去添加上看一下~

* 限制

有點像萬能比對符,寫在哪裡都可以

編寫規範 允許的版本範圍
* >=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
           

确定的版本号或範圍

福利:私信回複【01】可免費擷取python入門教程視訊
>= 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'"
           

依賴項的限制完全一樣,隻不過變成一行一個限制屬性,可讀性更強。最後,如果你的時間不是很緊張,并且又想快速的提高,最重要的是不怕吃苦,建議你可以價位@762459510 ,那個真的很不錯,很多人進步都很快,需要你不怕吃苦哦!大家可以去添加上看一下~

以上就是Python依賴管理及打包工具Poetry依賴規範的詳細内容,更多關于Python工具poetry依賴規範的資料請關注其它相關文章!

若文章對您有幫助,幫忙點個贊!

\