71

Currently, we are setting\installing up some packages on system by mentioning their version and dependencies in setup.py under install_requires attribute. Our system requires Python 2.7. Sometimes, users are having multiple versions of Python on their systems, say 2.6.x and 2.7, some packages it says are available already but actually on the system available under 2.6 site packages list. Also some users have 2.6 only, how to enforce from setup.py or is there any other way to say to have only Python 2.7 and all packages which we want setup.py to update are for only 2.7. We require minimum 2.7 on the machine to run our code.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Santhosh
  • 885
  • 1
  • 9
  • 15

2 Answers2

251

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.

Aaron V
  • 6,596
  • 5
  • 28
  • 31
  • 14
    Should be the official answer in my opinion. For one it describes current best practice and for two it describes possible limitations and lists version info. This is the superior answer. –  Jul 28 '18 at 22:29
  • 2
    The documentation linked in the answer above doesn't seem to have any information on the `python_requires` keyword - https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires (part of the same set of docs) describes it more directly – Peter Briggs Oct 04 '18 at 11:39
  • Thanks @PeterBriggs, updated the link (not sure if it was wrong in the first place or if the page moved ...) – Aaron V Nov 16 '18 at 17:42
  • 2
    I have pip 19.0.3, setuptools 41.0.1 and python 3.6.8. I've added `python_requires='>3.7' and pip is still happy to install the package but I think it should error as my Python version is too low. What else is needed? – Dan May 23 '19 at 13:36
  • I added `python_requires` to my setup.py and it simply doesn't work. The version with `if` seems ugly, but is more reliable from what I see. – ashrasmun Oct 29 '19 at 14:30
  • Relevant: https://packaging.python.org/guides/distributing-packages-using-setuptools/#classifiers, which mentions that "Although the list of classifiers is often used to declare what Python versions a project supports, this information is only used for searching & browsing projects on PyPI, not for installing projects. To actually restrict what Python versions a project can be installed on, use the python_requires argument." – 0 _ May 09 '21 at 10:00
  • Support for this feature is relatively recent. Your project’s source distributions and wheels must be built using at least version 24.2.0 of setuptools to use [python_requires](https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires) keyword. – Vincent Rouvreau Dec 06 '21 at 11:00
  • Using this in [this](https://github.com/Nelson-Gon/pyautocv/runs/7638353326?check_suite_focus=true) but it seems to treat greater than differently from what one might expect. – NelsonGon Aug 02 '22 at 18:55
20

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')
Community
  • 1
  • 1
Ewan
  • 14,592
  • 6
  • 48
  • 62