在我的诗歌项目中安装依赖项时,我遇到了一个问题.如果我运行poetry new
(如https://python-poetry.org/docs/basic-usage/中所述),我可以创建一个新项目:
$ poetry new scipy-test
Created package scipy_test in scipy-test
在删除了一些不需要复制的文件后,我的项目 struct 如下所示:
$ tree .
.
├── pyproject.toml
└── scipy_test
└── __init__.py
1 directory, 2 files
我的pyproject.toml
文件如下所示:
[tool.poetry]
name = "scipy-test"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
[tool.poetry.dependencies]
python = "^3.9"
[tool.poetry.dev-dependencies]
pytest = "^5.2"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
当我运行poetry add scipy
时,它try 安装最新版本的SciPy,现在是1.8.1.我得到以下错误:
$ poetry add scipy
Creating virtualenv scipy-test-4EDXm154-py3.9 in /home/mattwelke/.cache/pypoetry/virtualenvs
Using version ^1.8.1 for scipy
Updating dependencies
Resolving dependencies... (0.1s)
SolverProblemError
The current project's Python requirement (>=3.9,<4.0) is not compatible with some of the required packages Python requirement:
- scipy requires Python >=3.8,<3.11, so it will not be satisfied for Python >=3.11,<4.0
Because no versions of scipy match >1.8.1,<2.0.0
and scipy (1.8.1) requires Python >=3.8,<3.11, scipy is forbidden.
So, because scipy-test depends on scipy (^1.8.1), version solving failed.
at ~/.local/share/pypoetry/venv/lib/python3.9/site-packages/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 scipy, a possible solution would be to set the `python` property to ">=3.9,<3.11"
https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies,
https://python-poetry.org/docs/dependency-specification/#using-environment-markers
我将我的pyproject.toml
文件中的第python = "^3.9"
行解释为"这个项目本打算用Python 3.9(任何补丁版本)运行".我将包的Python要求">;=3.8,<;3.11"解释为"这个库需要Python 3.8、3.9或Python 3.10才能使用它".因此,如果我把这两件事放在一起,在我看来,它们应该相互兼容.
错误消息确实包含帮助我解决问题的提示.它说:
For scipy, a possible solution would be to set the `python` property to ">=3.8,<3.11"
我同意这将解决这个问题.这将使我的诗歌项目的Python版本与依赖项的Python需求完全匹配.我发现,如果我这样更改我的pyproject.toml
文件,我就可以安装依赖项.
但是我不想让我的项目用Python 3.8执行,我想把它的Python版本设置为我实际使用的最新版本.因此,我的首选版本实际上是"^3.10"(如果我对上述内容的理解正确的话),因为这意味着"您必须使用Python 3.10的任何补丁版本来运行它".
如果我将行更改为python = "^3.10"
,我会得到与之前相同的错误,除了错误消息中的提示提到版本3.10而不是3.8:
For scipy, a possible solution would be to set the `python` property to ">=3.10,<3.11"
如果我使用这个值,它会再次工作,允许我安装依赖项.这一次,它似乎限制了我的项目只与3.10兼容,正如所希望的那样.但它看起来有点冗长.我仍然不明白为什么设置为"^3.9"(或"^3.10")不起作用.
这里有什么我遗漏的吗?如果是这样,我将如何更改我的pyproject.toml
文件,使其与我要添加到项目中的依赖项兼容?