为什么我无法将 Python 要求 ">=3.8,<3.11" 的 Python 包安装到 Python 版本为“^3.9”的 Poetry 项目中?

Mat*_*lke 16 python scipy dependency-management python-3.x python-poetry

我在将依赖项安装到我的 Poetry 项目中时遇到问题。如果我运行(如https://python-poetry.org/docs/basic-usage/poetry new中所述),我可以创建一个新项目:

\n
$ poetry new scipy-test\nCreated package scipy_test in scipy-test\n
Run Code Online (Sandbox Code Playgroud)\n

删除本次复制不需要的一些文件后,我的项目结构如下所示:

\n
$ tree .\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scipy_test\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 __init__.py\n\n1 directory, 2 files\n
Run Code Online (Sandbox Code Playgroud)\n

我的pyproject.toml文件如下所示:

\n
[tool.poetry]\nname = "scipy-test"\nversion = "0.1.0"\ndescription = ""\nauthors = ["Your Name <you@example.com>"]\n\n[tool.poetry.dependencies]\npython = "^3.9"\n\n[tool.poetry.dev-dependencies]\npytest = "^5.2"\n\n[build-system]\nrequires = ["poetry-core>=1.0.0"]\nbuild-backend = "poetry.core.masonry.api"\n
Run Code Online (Sandbox Code Playgroud)\n

当我运行时poetry add scipy,它会尝试安装最新版本的 SciPy,目前是 1.8.1。我收到以下错误:

\n
$ poetry add scipy\nCreating virtualenv scipy-test-4EDXm154-py3.9 in /home/mattwelke/.cache/pypoetry/virtualenvs\nUsing version ^1.8.1 for scipy\n\nUpdating dependencies\nResolving dependencies... (0.1s)\n\n  SolverProblemError\n\n  The current project\'s Python requirement (>=3.9,<4.0) is not compatible with some of the required packages Python requirement:\n    - scipy requires Python >=3.8,<3.11, so it will not be satisfied for Python >=3.11,<4.0\n  \n  Because no versions of scipy match >1.8.1,<2.0.0\n   and scipy (1.8.1) requires Python >=3.8,<3.11, scipy is forbidden.\n  So, because scipy-test depends on scipy (^1.8.1), version solving failed.\n\n  at ~/.local/share/pypoetry/venv/lib/python3.9/site-packages/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 scipy, a possible solution would be to set the `python` property to ">=3.9,<3.11"\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.9"中的这一行解释pyproject.toml为“该项目意味着使用 Python 3.9(任何补丁版本)运行”。我将包的 Python 要求“>=3.8,<3.11”解释为“此库需要 Python 3.8、3.9 或 Python 3.10 才能使用它”。因此,如果我把这两件事放在一起,在我看来它们应该是兼容的。

\n

错误消息确实包含帮助我解决问题的提示。它说:

\n
For scipy, a possible solution would be to set the `python` property to ">=3.8,<3.11"\n
Run Code Online (Sandbox Code Playgroud)\n

我同意这可以解决问题。这将使我的 Poetry 项目的 Python 版本与依赖项的 Python 要求完全匹配。我发现如果我以pyproject.toml这种方式更改文件,我就能够安装依赖项。

\n

但我不想让我的项目使用 Python 3.8 执行。我想将其 Python 版本设置为我实际使用的最新版本。所以我的首选版本实际上是“^3.10”(如果我上面描述的理解是正确的),因为这应该意味着“你必须使用Python 3.10的任何补丁版本来运行它”。

\n

如果我将该行更改为python = "^3.10",我会收到与之前相同类型的错误,除了错误消息中的提示提到版本 3.10 而不是 3.8:

\n
For scipy, a possible solution would be to set the `python` property to ">=3.10,<3.11"\n
Run Code Online (Sandbox Code Playgroud)\n

如果我使用这个值,它会再次工作,允许我安装依赖项。这次,看起来它根据需要将我的项目限制为仅与 3.10 兼容。但看起来有点冗长。我仍然不明白为什么将其设置为“^3.9”(或“^3.10”)不起作用。

\n

我在这里缺少什么吗?如果是这样,我将如何更改我的pyproject.toml文件以使其与我想要添加到项目中的依赖项兼容?

\n

wkl*_*wkl 29

您指定的插入符要求...

[tool.poetry.dependencies]
python = "^3.9"
Run Code Online (Sandbox Code Playgroud)

...意味着“此 Python 代码具有兼容性3.9 <= python_version < 4”(^根据您指定版本的方式,使用语义版本控制进行不同的限制)。

这是比您的依赖项指定的更广泛的约束scipy,因为它声明仅兼容最高3.8 <= python_version < 3.11.

To poetry,scipy不满足您的约束,因为如果python3.11现在就退出,您的依赖约束会声明您的代码支持该 Python 版本,而scipy's 约束会说它不支持。

对于你来说,你可能只想要一个范围之类的东西来匹配类似 's 的东西scipy(或者缩小你的范围以适应scipy's 之类的东西)。

[tool.poetry.dependencies]
python = ">=3.10, <3.11"
Run Code Online (Sandbox Code Playgroud)

  • 这是有道理的。我误解了 ^ 字符的含义。我认为这是我使用 Node.js 的经验,我通常将其与我的依赖项一起使用,因为我想表达我的项目与新功能版本兼容。现在我意识到使用Python,我绝对不想表达我的项目与Python的新功能版本兼容。这些可能包含新的重大更改(Python 不使用 semver)。我现在明白我应该在我的项目中使用 `&gt;=3.10,&lt;3.11` 来表示它只能使用 Python 3.11(任何补丁版本)运行。谢谢! (2认同)