109

In my setup.py file, I've specified a few libraries needed to run my project:

setup(
    # ...
    install_requires = [
        'django-pipeline',
        'south'
    ]
)

How can I specify required versions of these libraries?

Tarsis Azevedo
  • 1,463
  • 14
  • 21
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

2 Answers2

166

I'm not sure about buildout, however, for setuptools/distribute, you specify version info with the comparison operators (like ==, >=, or <=).

For example:

install_requires = ['django-pipeline==1.1.22', 'south>=0.7']
Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
  • 2
    to undestand the setup.py better [read the docs](http://docs.python.org/distutils/setupscript.html) – Tarsis Azevedo Nov 17 '11 at 03:48
  • I know setup.py with distutils/setuptools/distribute, does buildout use this as well? I've never used it before, and wasn't sure why the OP mentioned buildout. – Adam Wagner Nov 17 '11 at 03:51
  • 4
    Buildout honors the `install_requires` entry of packages, including version requirements. It uses setuptools under the hood for this. – Martijn Pieters Nov 17 '11 at 07:13
  • Nice, thanks, that answers my question. I forgot that Buildout used setuptools under the hood. – Naftuli Kay Nov 17 '11 at 22:01
  • 2
    How can I specify the version of python? – qed Jul 27 '14 at 20:05
  • Does setup.py also honour the versions I have installed in my current env? Or does it just fetch the latest ones available? – Dev Aggarwal Sep 24 '18 at 16:43
  • 1
    @qed `python_requires='>=3',` [More information](https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires) – tuomastik Sep 28 '19 at 14:54
  • 5
    @TarsisAzevedo unfortunately the docs does not give these examples, as of April 2021. That page doesn’t even mention “ install_requires” – Tianyi Shi Apr 08 '21 at 11:32
  • what if I want to accept any patch (x.x.any) version? – Eliav Louski Feb 10 '22 at 02:43
  • I don't know how much of this might be new from the last comments, but there is documentation [on pypa.io](https://pip.pypa.io/en/stable/topics/dependency-resolution/) about dependency resolution, version operators (says to see [PEP-0440](https://peps.python.org/pep-0440/) for details in includes ranges like `==3.1.*`) python.org also [describes install_requires](https://docs.python.org/3/distutils/setupscript.html) on a different page. – snydergd Sep 14 '22 at 12:31
6

You can add them to your requirements.txt file along with the version.

For example:

django-pipeline==1.1.22
south>=0.7

and then in your setup.py

import os
from setuptools import setup

with open('requirements.txt') as f:
    required = f.read().splitlines()

setup(...
install_requires=required,
...)

Reading from the docs -

It is not considered best practice to use install_requires to pin dependencies to specific versions, or to specify sub-dependencies (i.e. dependencies of your dependencies). This is overly-restrictive, and prevents the user from gaining the benefit of dependency upgrades.

https://packaging.python.org/discussions/install-requires-vs-requirements/#id5

Gal Bracha
  • 19,004
  • 11
  • 72
  • 86