3

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 OS 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?

ppwater
  • 2,315
  • 4
  • 15
  • 29
Pranav Raj
  • 781
  • 1
  • 8
  • 19

2 Answers2

4

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.

osandov
  • 121
  • 1
  • 4
0

One possible approach:

$ cat latest_python_version.py
import requests
from bs4 import BeautifulSoup
import re

url = "https://www.python.org/doc/versions/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

# Search for
# <div class="section" id="python-documentation-by-version">
# ...
# </div>
div = soup.find("div", attrs={"id": "python-documentation-by-version"})

# Get the first link. It will be something like
# <li><a class="reference external" href="https://docs.python.org/release/3.11.0/">Python 3.11.0</a>,
#  documentation released on 24 October 2022.</li>
link = div.find("li")

# x.contents will be something like ['Python 3.11.0']
x = link.find("a", attrs={"href": re.compile("^https*://")})

# extract the latest version which will be something like '3.11.0'
matches = re.search("Python (.*)$", x.contents[0])
version = matches.group(1)
print(version)

Sample run:

$ python latest_python_version.py
3.11.0
Kamaraju Kusumanchi
  • 1,809
  • 19
  • 12