Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

install_requires based on python version

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?

like image 839
iTayb Avatar asked Jan 13 '14 00:01

iTayb


People also ask

What does install_requires do in setup py?

install_requires is a section within the setup.py file in which you need to input a list of the minimum dependencies needed for a project to run correctly on the target operating system (such as ubuntu). When pip runs setup.py, it will install all of the dependencies listed in install_requires.

How do I set python version in setup py?

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.

Do I need requirements txt if I have setup py?

If your package is developed only by yourself (i.e. on a single machine) but you are planning to redistribute it, then setup.py / setup. cfg should be enough. If you package is developed on multiple machines and you also need to redistribute it, you will need both the requirements. txt and setup.py / setup.

Is setup py required?

The short answer is that requirements. txt is for listing package requirements only. setup.py on the other hand is more like an installation script. If you don't plan on installing the python code, typically you would only need requirements.


2 Answers

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

like image 141
unholysampler Avatar answered Sep 25 '22 02:09

unholysampler


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) 
like image 32
sleepycal Avatar answered Sep 22 '22 02:09

sleepycal