0

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?

cbrnr
  • 1,564
  • 1
  • 14
  • 28
  • How can a dependency be optionnal ? If I was to use your library, would I need it or not ? – Itération 122442 May 03 '23 at 08:38
  • My package is not a library. This optional dependency is not required for its main functionality, but if you install it, you get additional functionality. I don't want to make it mandatory, because it is a huge dependency and not everyone is going to use it. – cbrnr May 03 '23 at 08:42
  • @Itération122442 - Easily, an application feature behind a paywall for example – Sayse May 03 '23 at 08:42
  • 1
    @Itération122442: Optional dependencies [are in fact a thing](https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies) in Python package distribution. – user2357112 May 03 '23 at 08:46
  • 1
    `tensorflow>=2.7.0; python_version<'3.11' and sys_platform=='darwin'` See https://stackoverflow.com/a/35614580/7976758 Found in https://stackoverflow.com/search?q=%5Bpip%5D+platform The problem is that `tensorflow` will always be installed when the condition is met, `pip` cannot consult existing packages to decide what to install. – phd May 03 '23 at 09:14
  • It seems like I need to add `tensorflow>=2.7.0; sys_platform != 'darwin'` to make this work, right? – cbrnr May 03 '23 at 09:20
  • @cbrnr If it's `tensorflow>=2.7.0` in both cases you don't need a conditional dependency. Just always install `tensorflow>=2.7.0`. – phd May 03 '23 at 10:24
  • It's the same dependency, but if on macOS, Python needs to be restricted to < 3.11. As explained in the accepted answer, this is currently not possible and can just be worked around. – cbrnr May 03 '23 at 10:34

1 Answers1

1

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.
  • Thanks, that's what I suspected. A bit unwieldy, but better than not being able to do it at all! – cbrnr May 03 '23 at 09:39
  • 1
    There are other "clever ways" of achieving that... For example, in the same "extra" group that you specify `tensorflow` as a optional dependency, you could add another dependency that has `python_requires = <=3.11` (you can create an empty package for that). This will force the depency resolution algorithm to fail if the conditions are not mat and the installation to halt. But 2 packages looks more clean to me. – Anderson Bravalheri May 03 '23 at 09:45