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

Des*_*eux 1 python python-poetry

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

\n

当我运行时,poetry add numpy出现以下错误:

\n
Using version ^1.22.4 for numpy\n\nUpdating dependencies\nResolving dependencies... (0.0s)\n\n  SolverProblemError\n\n  The current project's Python requirement (>=3.7,<4.0) is not compatible with some of the required packages Python requirement:\n    - numpy requires Python >=3.8, so it will not be satisfied for Python >=3.7,<3.8\n  \n  Because no versions of numpy match >1.22.4,<2.0.0\n   and numpy (1.22.4) requires Python >=3.8, numpy is forbidden.\n  So, because base-env depends on numpy (^1.22.4), version solving failed.\n\n  at ~/.poetry/lib/poetry/puzzle/solver.py:241 in _solve\n      237\xe2\x94\x82             packages = result.packages\n      238\xe2\x94\x82         except OverrideNeeded as e:\n      239\xe2\x94\x82             return self.solve_in_compatibility_mode(e.overrides, use_latest=use_latest)\n      240\xe2\x94\x82         except SolveFailure as e:\n    \xe2\x86\x92 241\xe2\x94\x82             raise SolverProblemError(e)\n      242\xe2\x94\x82 \n      243\xe2\x94\x82         results = dict(\n      244\xe2\x94\x82             depth_first_search(\n      245\xe2\x94\x82                 PackageNode(self._package, packages), aggregate_package_nodes\n\n  \xe2\x80\xa2 Check your dependencies Python requirement: The Python requirement can be specified via the `python` or `markers` properties\n    \n    For numpy, a possible solution would be to set the `python` property to ">=3.8,<4.0"\n\n    https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies,\n    https://python-poetry.org/docs/dependency-specification/#using-environment-markers\n
Run Code Online (Sandbox Code Playgroud)\n

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

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

    \n
  2. \n
  3. 如何poetry安装numpy与我的 Python 版本 ( ) 兼容的版本3.7

    \n
  4. \n
\n

我使用 来管理我的 Python 版本pyenv,因为我的全局 Python 版本设置为3.7.9poetry所以在初始化期间自动采用该版本:

\n
python --version\n> Python 3.7.9\n
Run Code Online (Sandbox Code Playgroud)\n

这就是我初始化我的pyproject.toml

\n
[tool.poetry]\nname = "base"\nversion = "0.1.0"\ndescription = "Base environment"\nauthors = ["pinco-pallo <pinco-pallo@email.com>"]\n\n[tool.poetry.dependencies]\npython = "^3.7"\n\n[tool.poetry.dev-dependencies]\n\n[build-system]\nrequires = ["poetry-core>=1.0.0"]\nbuild-backend = "poetry.core.masonry.api"\n
Run Code Online (Sandbox Code Playgroud)\n

fin*_*mer 5

没有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 找到支持它的 numpy 版本poetry add "numpy@~1.21"

如果您需要支持 python >=3.11,则必须在以下位置使用多个约束依赖项定义pyproject.toml

[tool.poetry.dependencies]
numpy = [
    {version = "<1.22", python = "<3.8"},
    {version = "^1.22", python = "^3.8"}
]
Run Code Online (Sandbox Code Playgroud)

编辑后poetry lock --no-update运行以锁定并安装依赖项。poetry installpyproject.toml