SageMaker Studio Python Library

1.1.11 · active · verified Sun Mar 29

The sagemaker-studio Python library is an open-source tool designed to interact with Amazon SageMaker Unified Studio resources. It provides a simplified interface to programmatically access and manage entities such as domains, projects, connections, and databases. The library also includes utility modules for common data operations, including SQL execution, DataFrame manipulation, and Spark session management. The current version is 1.1.11, and it maintains an active development and release cadence, with updates often reflecting the evolution of the broader SageMaker Unified Studio platform.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `ClientConfig`, `Domain`, and `Project` objects using the `sagemaker-studio` library. It highlights the importance of credential configuration, either automatically within SageMaker Studio JupyterLab or explicitly via `ClientConfig` with an AWS profile. It then shows how to retrieve basic properties of a SageMaker Domain and Project. Note that running this code successfully requires valid AWS credentials and existing SageMaker Domain and Project IDs, and appropriate IAM permissions.

import os
from sagemaker_studio import ClientConfig, Domain, Project

# --- Configure ClientConfig if not in SageMaker Studio JupyterLab ---
# In a SageMaker Studio JupyterLab environment, credentials are automatically pulled.
# Otherwise, provide AWS credentials and region.
# Replace 'us-east-1' and 'your-aws-profile' as needed.
# For demonstration, we use os.environ.get for robustness.
aws_region = os.environ.get('AWS_REGION', 'us-east-1')
aws_profile = os.environ.get('AWS_PROFILE', 'default')

# Instantiate ClientConfig (optional, if running outside of Studio or with specific profile)
try:
    client_config = ClientConfig(region_name=aws_region, profile_name=aws_profile)
except Exception as e:
    print(f"Could not initialize ClientConfig, proceeding assuming environment provides credentials: {e}")
    client_config = None # Or handle more robustly

# --- Interact with a SageMaker Domain and Project ---
# To interact with a Domain or Project, you typically need its ID.
# Replace 'your-domain-id' and 'your-project-id' with actual values.
# If running in Studio, these might be discoverable from the environment.
domain_id = os.environ.get('SAGEMAKER_DOMAIN_ID', 'd-xxxxxxxxxxxx') # Placeholder
project_id = os.environ.get('SAGEMAKER_PROJECT_ID', 'p-yyyyyyyyyyyy') # Placeholder

print(f"Attempting to interact with Domain ID: {domain_id}")
print(f"Attempting to interact with Project ID: {project_id}")

if client_config:
    # Initialize Domain with client_config
    domain = Domain(id=domain_id, client_config=client_config)
    project = Project(id=project_id, domain_id=domain_id, client_config=client_config)
else:
    # Initialize Domain without client_config (assumes environment provides)
    domain = Domain(id=domain_id)
    project = Project(id=project_id, domain_id=domain_id)

# Accessing some properties (these calls might fail if IDs are invalid or permissions are insufficient)
try:
    print(f"Domain ID: {domain.id}")
    print(f"Project Name: {project.name}")
    print(f"Project S3 Path: {project.s3_path}")
except Exception as e:
    print(f"Error accessing domain/project properties: {e}")
    print("Please ensure the provided Domain/Project IDs are valid and your AWS credentials/permissions are correctly configured.")

# Example of using a utility module (requires an actual connection and query context)
# from sagemaker_studio import sqlutils
# try:
#     result = sqlutils.sql("SELECT 1", connection_name="your-connection-name")
#     print(f"SQL Query Result: {result}")
# except Exception as e:
#     print(f"Error executing SQL query: {e}")

view raw JSON →