Looker SDK for Python

26.6.1 · active · verified Sun Mar 29

The `looker-sdk` is the official Python SDK for interacting with the Looker REST API. It provides a convenient, programmatic way to manage Looker instances, run queries, extract data, and automate workflows. Maintained by Google/Looker, it currently supports API 4.0 and requires Python 3.6+. It's actively developed with regular updates to support new Looker features and API versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Looker SDK using environment variables (recommended for security) and make a basic API call to retrieve information about the current user. It highlights the use of `init40()` for API 4.0, which is the current stable version. Ensure you replace placeholder environment variables with your actual Looker instance details and API credentials.

import os
import looker_sdk

# --- Configuration using Environment Variables ---
# Set these environment variables before running:
# LOOKERSDK_BASE_URL: Your Looker instance URL (e.g., https://your.looker.com or https://your.looker.cloud.google.com)
#   Note: AWS-hosted instances may require ':19999' port (e.g., https://your.looker.com:19999), while GCP-hosted instances usually omit it.
# LOOKERSDK_API_VERSION: Should be '4.0'
# LOOKERSDK_VERIFY_SSL: 'true' or 'false'
# LOOKERSDK_CLIENT_ID: Your API3 Client ID
# LOOKERSDK_CLIENT_SECRET: Your API3 Client Secret
# LOOKERSDK_TIMEOUT: (Optional) Request timeout in seconds, default is 120.

# Example of setting environment variables (replace with your actual values):
os.environ['LOOKERSDK_BASE_URL'] = os.environ.get('LOOKERSDK_BASE_URL', 'https://your.looker.com')
os.environ['LOOKERSDK_API_VERSION'] = os.environ.get('LOOKERSDK_API_VERSION', '4.0')
os.environ['LOOKERSDK_VERIFY_SSL'] = os.environ.get('LOOKERSDK_VERIFY_SSL', 'true')
os.environ['LOOKERSDK_CLIENT_ID'] = os.environ.get('LOOKERSDK_CLIENT_ID', 'YOUR_CLIENT_ID')
os.environ['LOOKERSDK_CLIENT_SECRET'] = os.environ.get('LOOKERSDK_CLIENT_SECRET', 'YOUR_CLIENT_SECRET')

# Initialize the SDK for API 4.0
try:
    sdk = looker_sdk.init40()
    print("Looker SDK 4.0 initialized successfully.")

    # Make a simple API call to get the current user
    me = sdk.me()
    print(f"Hello, {me.first_name} {me.last_name} ({me.email})!")

    # Example of creating a user (requires appropriate permissions)
    # from looker_sdk import models
    # new_user_data = models.WriteUser(first_name="Test", last_name="User", email="test.user@example.com")
    # new_user = sdk.create_user(body=new_user_data)
    # print(f"Created new user: {new_user.first_name}")

except Exception as e:
    print(f"Error initializing SDK or making API call: {e}")
    print("Please ensure environment variables (LOOKERSDK_BASE_URL, LOOKERSDK_API_VERSION, CLIENT_ID, CLIENT_SECRET) are correctly set.")

view raw JSON →