PyPI API - How to get stable package version

Version scheme defined in the PEP-440. There is a module packaging, which can handle version parsing and comparison.

I came up with this function to get latest stable version of a package:

import requests
import json
try:
    from packaging.version import parse
except ImportError:
    from pip._vendor.packaging.version import parse


URL_PATTERN = 'https://pypi.python.org/pypi/{package}/json'


def get_version(package, url_pattern=URL_PATTERN):
    """Return version of package on pypi.python.org using json."""
    req = requests.get(url_pattern.format(package=package))
    version = parse('0')
    if req.status_code == requests.codes.ok:
        j = json.loads(req.text.encode(req.encoding))
        releases = j.get('releases', [])
        for release in releases:
            ver = parse(release)
            if not ver.is_prerelease:
                version = max(version, ver)
    return version


if __name__ == '__main__':
    print("Django==%s" % get_version('Django'))

When executed, this produces following results:

$ python v.py
Django==2.0

Just a quick note (as I can't add a reply to a previous answer yet) that sashk's answer could return an incorrect answer, as max() doesnt really understand versioning, f.e. as of now on SQLAlchemy it thinks 1.1.9 is higher than 1.1.14 (which is actually the latest stable release).

quick solution:

import urllib.request
import packaging.version
import distutils.version
data = json.loads(urllib.request.urlopen('https://pypi.python.org/pypi/SQLAlchemy/json').readall().decode('utf-8'))
max([distutils.version.LooseVersion(release) for release in data['releases'] if not packaging.version.parse(release).is_prerelease])

which correctly returns

LooseVersion ('1.1.14')

Tags:

Python

Pip

Pypi