install_requires based on python version

PythonSetuptoolsDistutilsDistributeInstall Requires

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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).

Solution 2 - Python

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniTaybView Question on Stackoverflow
Solution 1 - PythonunholysamplerView Answer on Stackoverflow
Solution 2 - PythonsleepycalView Answer on Stackoverflow