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
- Obtain the Posit Connect server URL and API Key from environment variables.
- Obtain your local Python version using
sys.version_info
. - Retrieve the set of known Python installations with the
GET /v1/server_settings/python
endpoint. - Parse the response using
requests.Response.json
. - 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.
= os.getenv("CONNECT_SERVER")
connectServer = os.getenv("CONNECT_API_KEY")
connectAPIKey
= ".".join(map(str, sys.version_info[:3]))
myPythonVersion = requests.get(
resp + "__api__/v1/server_settings/python",
connectServer = {
headers "Authorization": "Key " + connectAPIKey,
},
)= resp.json()
payload = (each["version"] for each in payload["installations"])
versions 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
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
- Obtain the Posit Connect server URL and API Key from environment variables.
- Obtain your local Python version using
sys.version_info
. - Retrieve the set of known Python installations with the
GET /v1/server_settings/python
endpoint. - Parse the response using
requests.Response.json
. - 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.
= os.getenv("CONNECT_SERVER")
connectServer = os.getenv("CONNECT_API_KEY")
connectAPIKey
= ".".join(map(str, sys.version_info[:3]))
myPythonVersion = '3.8.12'
myPythonVersion = requests.get(
resp + "__api__/v1/server_settings/python",
connectServer = {
headers "Authorization": "Key " + connectAPIKey,
},
)= resp.json()
payload = [each["image_name"] for each in payload["installations"] if each["version"] == myPythonVersion]
matching 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")