Databricks SDK for Python

0.102.0 · active · verified Sat Mar 28

The Databricks SDK for Python (Beta) provides a comprehensive client for interacting with the Databricks Lakehouse. It covers all public Databricks REST API operations, offering a robust internal HTTP client that handles intelligent retries. While in Beta, it is supported for production use cases, though future releases are expected to introduce some interface changes. The library is actively developed with frequent releases.

Warnings

Install

Imports

Quickstart

This quickstart initializes a `WorkspaceClient` and lists all clusters in the configured Databricks workspace. It leverages the unified authentication mechanism, which automatically detects credentials from environment variables (`DATABRICKS_HOST`, `DATABRICKS_TOKEN`) or a `.databrickscfg` file.

import os
from databricks.sdk import WorkspaceClient

# Databricks SDK uses Databricks unified authentication.
# It prioritizes environment variables (DATABRICKS_HOST, DATABRICKS_TOKEN)
# or a .databrickscfg file. For this example to run outside Databricks,
# ensure these are set.
# Example: export DATABRICKS_HOST=https://your-workspace.cloud.databricks.com
# Example: export DATABRICKS_TOKEN=dapi********************************

host = os.environ.get('DATABRICKS_HOST', 'https://your-workspace.cloud.databricks.com')
token = os.environ.get('DATABRICKS_TOKEN', 'dapi_your_token_here')

try:
    # Initialize WorkspaceClient, which will pick up credentials automatically.
    # For explicit config:
    # w = WorkspaceClient(host=host, token=token)
    w = WorkspaceClient()

    print(f"Listing clusters in Databricks workspace: {w.config.host}")
    for c in w.clusters.list():
        print(f"  - {c.cluster_name} (ID: {c.cluster_id})")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure DATABRICKS_HOST and DATABRICKS_TOKEN environment variables are set ")
    print("or a valid .databrickscfg file exists with proper authentication.")

view raw JSON →