Python Versions
These recipes 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 Connect server.
Python versions available to Posit Connect
This recipe compares your local Python version against the Python installations available on your Connect server. It uses the GET /v1/server_settings/python
endpoint to obtain the Python installations available to Connect.
Workflow
- Obtain the 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 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.
= os.getenv("CONNECT_SERVER")
connect_server = os.getenv("CONNECT_API_KEY")
connect_api_key
= ".".join(map(str, sys.version_info[:3]))
my_python_version = requests.get(
resp f"{connect_server}__api__/v1/server_settings/python",
= {
headers "Authorization": f"Key {connect_api_key}",
},
)= resp.json()
payload = (each["version"] for each in payload["installations"])
versions 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 Connect installation uses off-host content execution with Kubernetes, Connect will be configured with one or more images which might include different versions of Python.
This recipe compares your local Python version against the Python installations available on your Connect server’s configured images. It uses the GET /v1/server_settings/python
endpoint to obtain the Python installations available to 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 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’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.
= os.getenv("CONNECT_SERVER")
connect_server = os.getenv("CONNECT_API_KEY")
connect_api_key
= ".".join(map(str, sys.version_info[:3]))
my_python_version = requests.get(
resp f"{connect_server}__api__/v1/server_settings/python",
= {
headers "Authorization": f"Key {connect_api_key}",
},
)= resp.json()
payload = [each["image_name"] for each in payload["installations"] if each["version"] == my_python_version]
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 local Python version {my_python_version} in any available images")