I need a reliable way to retrieve a list of all released versions of Python (only final releases) to date.
Right now I'm relying on the tags of the CPython GitHub repository (see my added answer), but this seems a bit hacky. Isn't there an official way (e.g. a web-API) to fetch all current Python releases?
Use GitHub API to get the tags of the CPython GitHub repository, then filter them using regex:
import requests
import re
# Get all tags
tags = requests.get("https://api.github.com/repos/python/cpython/git/refs/tags").json()
python_versions = dict()
for tag in tags:
# Match tags that represent final versions, i.e. vN.N.N where Ns are integers
match = re.match(r'^refs/tags/v(\d+\.\d+\.\d+)$', tag['ref'])
if match:
major, minor, patch = match.group(1).split(".")
major_dict = python_versions.setdefault(major, dict())
minor_dict = major_dict.setdefault(minor, list())
minor_dict.append(patch)
This creates a dictionary where the keys are the major release versions:
>>> list(python_versions.keys())
['0', '1', '2', '3']
and the values are dictionaries themselves, with keys being minor release versions, and values being a list of corresponding patch versions:
>>> list(python_versions['3'].keys())
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']
>>> python_versions['3']['11']
['0', '1', '2', '3', '4']