Search code examples
pythonpython-3.xpip

Getting the latest Python 3 version programmatically


I want to get the latest Python source from https://www.python.org/ftp/python/.

While posting this, the latest version is 3.9.1. I do not want to hardcode 3.9.1 in my code to get the latest version and keep on updating the version when a new version comes out. I am using Ubuntu 16.04.

Is there a programmatic way to get the latest Python version (using curl) and use that version to get the latest source?


Solution

  • I had a similar problem and couldn't find anything better than scraping the downloads page. You mentioned curl, so I'm assuming you want a shell script. I ended up with this:

    url='https://www.python.org/ftp/python/'
    
    curl --silent "$url" |
        sed -n 's!.*href="\([0-9]\+\.[0-9]\+\.[0-9]\+\)/".*!\1!p' |
        sort -rV |
    while read -r version; do
        filename="Python-$version.tar.xz"
        # Versions which only have alpha, beta, or rc releases will fail here.
        # Stop when we find one with a final release.
        if curl --fail --silent -O "$url/$version/$filename"; then
            echo "$filename"
            break
        fi
    done
    

    This relies on sort -V, which I believe is specific to GNU coreutils. That shouldn't be a problem on Ubuntu.

    If you're using this in a larger script and want to use the version or filename variables after the loop, see How to pipe input to a Bash while loop and preserve variables after loop ends.