Search code examples
pythonsetuptoolspython-packaging

How do I specify a Python version constraint for a dependency specifically for a platform?


My Python package requires Python >= 3.8 (defined in setup.cfg or pyproject.toml):

python_requires = >=3.8

However, it also has the following optional dependency :

tensorflow>=2.7.0

If optional dependencies are tob be installed, I would like to require Python < 3.11 on macOS only. Previously, I tried:

tensorflow>=2.7.0;python_version<'3.11'

But that contrains Python on all platforms. Is there a way to achieve this?


Solution

  • Currently, it is not possible to specify setup.cfg' python_requires or pyproject.toml' requires-python in terms of the package dependencies. Just the otherway around: you can specify dependencies in terms of the Python version using environment markers as the example you mentioned in the question.

    If you want to workaround this limitation of the Python packaging system, one possible way is to create 2 separated packages:

    • "pkg A", with the basic dependencies, python_requires = >=3.8, and without tensorflow.
    • "pkg B", with tensorflow and "pkg A" as dependencies and python_requires = <=3.11.