Search code examples
pythonpython-poetry

poetry: first added library version higher than allowed by project python version


I've created a new environment using poetry and python3.7 and am trying to install the first package in the project: numpy.

When I run poetry add numpy I get the following error:

Using version ^1.22.4 for numpy

Updating dependencies
Resolving dependencies... (0.0s)

  SolverProblemError

  The current project's Python requirement (>=3.7,<4.0) is not compatible with some of the required packages Python requirement:
    - numpy requires Python >=3.8, so it will not be satisfied for Python >=3.7,<3.8
  
  Because no versions of numpy match >1.22.4,<2.0.0
   and numpy (1.22.4) requires Python >=3.8, numpy is forbidden.
  So, because base-env depends on numpy (^1.22.4), 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 numpy, a possible solution would be to set the `python` property to ">=3.8,<4.0"

    https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies,
    https://python-poetry.org/docs/dependency-specification/#using-environment-markers

The instructions in the stack trace advise me to change my Python version to 3.8, but I need to stay within 3.7. So I have two questions:

  1. Why does poetry attempt to use a numpy version that is incompatible with my Python version?

  2. How do I get poetry to install a numpy version that is compatible with my Python version (3.7)?

I manage my Python versions with pyenv and since my global Python version is set to 3.7.9, poetry automatically assumes that version during intialization:

python --version
> Python 3.7.9

This is how I initialized my pyproject.toml:

[tool.poetry]
name = "base"
version = "0.1.0"
description = "Base environment"
authors = ["pinco-pallo <pinco-pallo@email.com>"]

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

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

Solution

  • There is no numpy release that fulfill the projects python requirement (>=3.7,<4.0).

    • numpy >=1.22 requires python >=3.8
    • numpy >=1.21, <1.22 requires python >=3.7,<3.11

    You can pin the projects python requirement to >=3.7,<3.11 and help Poetry finding the numpy version that support it by poetry add "numpy@~1.21".

    If you need to support python >=3.11 you have to use multiple constraints dependency definitions in the pyproject.toml:

    [tool.poetry.dependencies]
    numpy = [
        {version = "<1.22", python = "<3.8"},
        {version = "^1.22", python = "^3.8"}
    ]
    

    Run poetry lock --no-update and poetry install after editing your pyproject.toml to lock and install the dependencies.