簡體   English   中英

poetry管理的python>=3.5項目下如何使用python black formatter?

[英]How to use python black formatter under a project for python>=3.5 managed by poetry?

我用 Poetry 創建了一個 python 項目“foo”。 這是pyproject.toml的內容:

[tool.poetry]
name = "bar"
version = "0.1.0"
description = ""

[tool.poetry.dependencies]
python = ">=3.5"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

這個package兼容Python3.5。 我想格式化程序,它與 Python3.5 不兼容。 我覺得用Python>=3.6開發是沒有問題的,但是無法安裝black formatter:

$ poetry add black --dev
[SolverProblemError]
The current project's Python requirement (>=3.5) is not compatible with some of the required packages Python requirement:
  - black requires Python >=3.6

Because no versions of black match >19.10b0,<20.0
 and black (19.10b0) requires Python >=3.6, black is forbidden.
So, because bar depends on black (^19.10b0), version solving failed.

所以我直接用pip安裝了黑色:

$ poetry run pip install black

這種方式不適合我。 欲以詩裝black

我應該怎么做? (我不想修改對python>=3.6的依賴)

似乎有點晚了,但實際上你可以做你想做的,即使黑色只支持 Python >=3.6.2

在您的 pyproject.toml 中,您可以定義受限制的依賴項,如https 中所述://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies

[tool.poetry.dependencies]
python = ">=3.5"

[tool.poetry.dev-dependencies]
black = {version = "^21.7b0", python = ">=3.6.2"}

詩歌不會抱怨,你也不會有任何問題,因為它是一種開發依賴。

您需要在 pyproject.toml 中編輯pyproject.toml值:

[tool.poetry]
name = "bar"
version = "0.1.0"
description = ""

[tool.poetry.dependencies]
python = ">=3.6"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM