24
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

poetryのpython version指定で沼った話

Last updated at Posted at 2022-10-24

何が起きた?

pythonのrepositoryで別のpythonのrepositoryをsubmoduleで参照しておりました。

そして以下のような記述をしておりました。

project-Aのpyproject.toml
[tool.poetry.dependencies]
python = ">=3.7.1, <3.11"
project-B(submodule)のpyproject.toml
[tool.poetry.dependencies]
python = "^3.10"

project-Aでは3.7.1以上3.11未満のpythonバージョンを指定、project-Bでは3.10以上4.0.0未満のバージョンを指定しているので範囲内に両方を満たすバージョンがあるのでpoetryがよしなに解決してpython 3.10.xみたいなのを参照してくれるかと思っておりました。

しかし以下のエラーが出ました。

SolverProblemError

  The current project's Python requirement (>=3.7.1,<3.11) is not compatible with some of the required packages Python requirement:
    - project-B requires Python ^3.10, so it will not be satisfied for Python >=3.7.1,<3.10
  
  Because project-A depends on project-B (0.1.0) which requires Python ^3.10, 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 project-B, a possible solution would be to set the `python` property to ">=3.10,<3.11"

エラーとしてはバージョン解決エラー(SolverProblemError)のようです。なぜでしょう?

For project-B, a possible solution would be to set the python property to ">=3.10,<3.11"

と出ているのでproject-Bのpython versionを">=3.10,<3.11"にしてあげて、つまりproject-Aのpythonバージョン内にしてあげてと書いてあります。

結論

poetry's version solving is fundamental different to other programs in that way, that it tries to find a major version of a package, that is compatible to all python versions, to which the current project is compatible, and not just the one you are currently using.

このissueに書いてある通りpoetryのversion解決は他のパッケージマネージャーと異なり、指定した全てのmajor versionでcompatible(互換性がある)必要があるそうです。

ですのでproject-Bのpython versionはproject-Aのpython versionに内包(project-A ⊂ project-Bの関係)されてないといけません。言い換えると全てのAのpython versionでBは動ける必要があるということです。

つまり今回のケースだと

project-Aのpyproject.toml
[tool.poetry.dependencies]
python = ">=3.10.0, <3.11"
# package B: python = "^3.10"

にする必要があります。

逆にproject-B ⊂ project-Aでも動きません。

project-Bのpyproject.toml
[tool.poetry.dependencies]
python = "3.10"
# package A: python = ">=3.7.1, <3.11"

この記事で誰かを沼から救えたら幸いです。

24
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?