Search code examples
pythonsetuptools

install_requires in setup.py depending on installed Python version


My setup.py looks something like this:

from distutils.core import setup

setup(
    [...]
    install_requires=['gevent', 'ssl', 'configobj', 'simplejson', 'mechanize'],
    [...]
)

Under Python 2.6 (or higher) the installation of the ssl module fails with:

ValueError: This extension should not be used with Python 2.6 or later (already built in), and has not been tested with Python 2.3.4 or earlier.

Is there a standard way to define dependencies only for specific python versions? Of course I could do it with if float(sys.version[:3]) < 2.6: but maybe there is a better way to do it.


Solution

  • It's just a list, so somewhere above you have to conditionally build a list. Something like to following is commonly done.

    import sys
    
    if sys.version_info < (2 , 6):
        REQUIRES = ['gevent', 'ssl', 'configobj', 'simplejson', 'mechanize'],
    else:
        REQUIRES = ['gevent', 'configobj', 'simplejson', 'mechanize'],
    
    setup(
    # [...]
        install_requires=REQUIRES,
    # [...]
    )