诗歌:首先添加的库版本高于项目 python 版本允许的版本

poetry: first added library version higher than allowed by project python version

我已经使用 poetrypython3.7 创建了一个新环境,并且正在尝试在项目中安装第一个包:numpy.

当我 运行 poetry add numpy 我得到以下错误:

Using version ^1.22.4 for numpy

Updating dependencies
Resolving dependencies... (0.0s)

  SolverProblemError

  The current project's Python requirement (>=3.7,<4.0) is not compatible with some of the required packages Python requirement:
    - numpy requires Python >=3.8, so it will not be satisfied for Python >=3.7,<3.8
  
  Because no versions of numpy match >1.22.4,<2.0.0
   and numpy (1.22.4) requires Python >=3.8, numpy is forbidden.
  So, because base-env depends on numpy (^1.22.4), version solving failed.

  at ~/.poetry/lib/poetry/puzzle/solver.py:241 in _solve
      237│             packages = result.packages
      238│         except OverrideNeeded as e:
      239│             return self.solve_in_compatibility_mode(e.overrides, use_latest=use_latest)
      240│         except SolveFailure as e:
    → 241│             raise SolverProblemError(e)
      242│ 
      243│         results = dict(
      244│             depth_first_search(
      245│                 PackageNode(self._package, packages), aggregate_package_nodes

  • Check your dependencies Python requirement: The Python requirement can be specified via the `python` or `markers` properties
    
    For numpy, a possible solution would be to set the `python` property to ">=3.8,<4.0"

    https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies,
    https://python-poetry.org/docs/dependency-specification/#using-environment-markers

堆栈跟踪中的说明建议我将 Python 版本更改为 3.8,但我需要保持在 3.7 内。所以我有两个问题:

  1. 为什么 poetry 尝试使用与我的 Python 版本不兼容的 numpy 版本?

  2. 如何让 poetry 安装与我的 Python 版本 (3.7) 兼容的 numpy 版本?

我使用 pyenv 管理我的 Python 版本,并且由于我的全局 Python 版本设置为 3.7.9poetry 在初始化期间自动采用该版本:

python --version
> Python 3.7.9

这就是我初始化 pyproject.toml 的方式:

[tool.poetry]
name = "base"
version = "0.1.0"
description = "Base environment"
authors = ["pinco-pallo <pinco-pallo@email.com>"]

[tool.poetry.dependencies]
python = "^3.7"

[tool.poetry.dev-dependencies]

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

没有 numpy 版本满足项目 python 要求 (>=3.7,<4.0)。

  • numpy >=1.22 要求 python >=3.8
  • numpy >=1.21, <1.22 要求 python >=3.7,<3.11

您可以将项目 python 要求固定到 >=3.7,<3.11 并通过 poetry add "numpy@~1.21" 帮助 Poetry 找到支持它的 numpy 版本。

如果你需要支持 python >=3.11 你必须在 pyproject.toml:

中使用 multiple constraints dependency definitions
[tool.poetry.dependencies]
numpy = [
    {version = "<1.22", python = "<3.8"},
    {version = "^1.22", python = "^3.8"}
]

运行 poetry lock --no-updatepoetry install 在编辑您的 pyproject.toml 以锁定并安装依赖项之后。