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 R and Python 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 connectServer URL must have a trailing slash.
connectServer = os.getenv("CONNECT_SERVER")
connectAPIKey = os.getenv("CONNECT_API_KEY")

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

Python Versions Available in Content Execution Images

Note

This section describes a feature that is currently in beta. If you are not sure if this applies to you, please speak with your administrator.

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 R version. If it is not listed, the Posit Connect server’s images do not contain the local R version.

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

import os
import requests
import sys

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

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