117

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?

3 Answers 3

173

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

See the Python packaging documentation

Sign up to request clarification or add additional context in comments.

10 Comments

to undestand the setup.py better read the docs
Buildout honors the install_requires entry of packages, including version requirements. It uses setuptools under the hood for this.
How can I specify the version of python?
@qed python_requires='>=3', More information
@TarsisAzevedo unfortunately the docs does not give these examples, as of April 2021. That page doesn’t even mention “ install_requires”
|
9

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

Comments

0

SemVer options

  install_requires=[

    #-- the `~` range is supported:
    'py-consul ~= 1.5', # means >=1.5.0, <1.6

    #-- the `^` range is not supported, emulate:
    'requests >=2.10, <3.0',
  ],

Spec: https://packaging.python.org/en/latest/specifications/version-specifiers/#id5

Quick reminder:

  1. In install_requires (setup_requires, tests_require), package authors declare the widest acceptable range of their dependencies' versions. That's to provide maximum reasonable flexibility to downstream package consumers in their dependencies.

  2. In requirements.txt, you should always keep completely pinned install plan, as if from pip freeze. That's to ensure reproducible build (similar to lockfiles in packaging systems of other languages).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.