在我的诗歌项目中安装依赖项时,我遇到了一个问题.如果我运行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要求"&gt;=3.8,&lt;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文件,使其与我要添加到项目中的依赖项兼容?

推荐答案

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

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

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

这是一个比依赖项scipy指定的范围更广的约束,因为它只声称兼容性高达3.8 <= python_version < 3.11.

poetryscipy不满足您的约束,因为如果python3.11现在不存在,您的依赖约束会声明您的代码支持该Python版本,而scipy的约束会说它不支持.

对于你来说,你可能只是想要一个与scipy相匹配的范围(或者缩小你的范围以适应scipy以内的范围).

[tool.poetry.dependencies]
python = ">=3.10, <3.11"

Python相关问答推荐

我们可以在apps.py?中使用Post_Save信号吗

Tokenizer Docker:无法为Tokenizer构建轮子,这是安装pyproject.toml项目所需的

如何在Pandas 中存储二进制数?

Pandas 群内滚动总和

socket.gaierror:[Errno -2]名称或服务未知|Firebase x Raspberry Pi

Polars -转换为PL后无法计算熵.列表

在编写要Excel的数据透视框架时修复标题行

如何使用Python中的clinicalTrials.gov API获取完整结果?

Python中的函数中是否有充分的理由接受float而不接受int?

Python -Polars库中的滚动索引?

如何让Flask 中的请求标签发挥作用

用合并列替换现有列并重命名

Python键入协议默认值

如何获取numpy数组的特定索引值?

如何在WSL2中更新Python到最新版本(3.12.2)?

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

如何更新pandas DataFrame上列标题的de值?

需要帮助重新调整python fill_between与数据点

幂集,其中每个元素可以是正或负""""