Python Versions

These recipes will help you determine if the version of Python you are using locally is available to Posit Connect.

Use the Content Runtimes recipe to enumerate the Python and R versions used by content deployed to your Posit Connect server.

Python Versions Available to Posit Connect

This recipe compares your local Python version against the Python installations available on your Posit Connect server. It uses the GET /v1/server_settings/python endpoint to obtain the Python installations available to Posit Connect.

Workflow

  1. Obtain the Posit Connect server URL and API Key from environment variables.
  2. Obtain your local Python version using sys.version_info.
  3. Retrieve the set of known Python installations with the GET /v1/server_settings/python endpoint.
  4. Parse the response using requests.Response.json.
  5. Check the response for the local Python version. If it is not listed, the Posit Connect server does not contain the local Python version.

Here is an example of the workflow using the requests package:

import os
import requests
import sys

# The connect_server URL must have a trailing slash.
connect_server = os.getenv("CONNECT_SERVER")
connect_api_key = os.getenv("CONNECT_API_KEY")

my_python_version = ".".join(map(str, sys.version_info[:3]))
resp = requests.get(
    f"{connect_server}__api__/v1/server_settings/python",
    headers = {
        "Authorization": f"Key {connect_api_key}",
    },
)
payload = resp.json()
versions = (each["version"] for each in payload["installations"])
if my_python_version in versions:
    print("The local Python version was found on the Posit Connect server")
else:
    print(f"Cannot find local Python version {my_python_version} on the Posit Connect server")

Python Versions Available in Content Execution Images

If your Posit Connect installation uses off-host content execution with Kubernetes, Connect will be configured with one or more images which may include different versions of Python.

This recipe compares your local Python version against the Python installations available on your Posit Connect server’s configured images. It uses the GET /v1/server_settings/python endpoint to obtain the Python installations available to Posit Connect, and find which images they are available on. You can use the same pattern to search for R or Quarto installations.

Workflow

  1. Obtain the Posit Connect server URL and API Key from environment variables.
  2. Obtain your local Python version using sys.version_info.
  3. Retrieve the set of known Python installations with the GET /v1/server_settings/python endpoint.
  4. Parse the response using requests.Response.json.
  5. Check the response for the local Python version. If it is not listed, the Posit Connect server’s images do not contain the local Python version.

Here is an example of this workflow using the requests package:

import os
import requests
import sys

# The connect_server URL must have a trailing slash.
connect_server = os.getenv("CONNECT_SERVER")
connect_api_key = os.getenv("CONNECT_API_KEY")

my_python_version = ".".join(map(str, sys.version_info[:3]))
resp = requests.get(
    f"{connect_server}__api__/v1/server_settings/python",
    headers = {
        "Authorization": f"Key {connect_api_key}",
    },
)
payload = resp.json()
matching = [each["image_name"] for each in payload["installations"] if each["version"] == my_python_version]
if matching:
    print("The local Python version was found in the following images:")
    for each in matching:
        print(each)
else:
    print(f"Cannot find local Python version {my_python_version} in any available images")