How can I specify library versions in setup.py?

PythonBuildout

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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']

Solution 2 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaftuli KayView Question on Stackoverflow
Solution 1 - PythonAdam WagnerView Answer on Stackoverflow
Solution 2 - PythonGal BrachaView Answer on Stackoverflow