Enforcing python version in setup.py

The current best practice (as of this writing in March 2018) is to add a python_requires argument directly to the setup() call in setup.py:

from setuptools import setup

[...]

setup(name="my_package_name",
      python_requires='>3.5.2',
      [...]

Note that this requires setuptools>=24.2.0 and pip>=9.0.0; see the documentation for more information.


As the setup.py file is installed via pip (and pip itself is run by the python interpreter) it is not possible to specify which Python version to use in the setup.py file.

Instead have a look at this answer to setup.py: restrict the allowable version of the python interpreter which has a basic workaround to stop the install.

In your case the code would be:

import sys
if sys.version_info < (2,7):
    sys.exit('Sorry, Python < 2.7 is not supported')