59

I have a module that works both on python 2 and python 3. In Python<3.2 I would like to install a specific package as a dependency. For Python>=3.2.

Something like:

 install_requires=[
    "threadpool >= 1.2.7 if python_version < 3.2.0",
 ],

How can one make that?

iTayb
  • 12,373
  • 24
  • 81
  • 135

2 Answers2

110

Use environment markers:

install_requires=[
    'threadpool >= 1.2.7; python_version < "3.2.0"',
]

Setuptools specific usage is detailed in their documentation. The syntax shown above requires setuptools v36.2+ (change log).

mipadi
  • 398,885
  • 90
  • 523
  • 479
unholysampler
  • 17,141
  • 7
  • 47
  • 64
8

This has been discussed here, it would appear the recommend way is to test for the Python version inside your setup.py using sys.version_info;

import sys

if sys.version_info >= (3,2):
    install_requires = ["threadpool >= 1.2.7"]
else:
    install_requires = ["threadpool >= 1.2.3"]

setup(..., install_requires=install_requires)
rescdsk
  • 8,739
  • 4
  • 36
  • 32
SleepyCal
  • 5,739
  • 5
  • 33
  • 47
  • 20
    This solution is fragile with many combinations of `pip` and `wheel` packages. When pip builds wheels on your behalf, the computed install_requires list is written in the wheel metadata and then the cached wheel might be used on a different Python version. – Marius Gedminas Dec 29 '16 at 07:37
  • 1
    Thanks. This used to work for me, then stopped. With your comment I was able to figure out it was because I recently changed my publish command to `bdist_wheel`. – Gringo Suave Oct 16 '18 at 01:56
  • It will not generate the correct package metadata this way. **Avoid**. – wim Sep 06 '20 at 20:40