How can I specify library versions in setup.py?
Asked Answered
B

2

112

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?

Bremen answered 17/11, 2011 at 3:6 Comment(0)
S
169

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

Steverson answered 17/11, 2011 at 3:40 Comment(10)
to undestand the setup.py better read the docsKano
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.Steverson
Buildout honors the install_requires entry of packages, including version requirements. It uses setuptools under the hood for this.Oringas
Nice, thanks, that answers my question. I forgot that Buildout used setuptools under the hood.Bremen
How can I specify the version of python?Sparkle
Does setup.py also honour the versions I have installed in my current env? Or does it just fetch the latest ones available?Avron
@Sparkle python_requires='>=3', More informationReichenberg
@TarsisAzevedo unfortunately the docs does not give these examples, as of April 2021. That page doesn’t even mention “ install_requires”Doscher
what if I want to accept any patch (x.x.any) version?Sylvester
I don't know how much of this might be new from the last comments, but there is documentation on pypa.io about dependency resolution, version operators (says to see PEP-0440 for details in includes ranges like ==3.1.*) python.org also describes install_requires on a different page.Unstop
A
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

Asexual answered 2/9, 2021 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.