Monte Carlo Python SDK (Pycarlo)
Pycarlo is Monte Carlo's Python SDK, providing programmatic access to the Monte Carlo data observability platform's APIs. It enables users to execute queries and mutations against the Monte Carlo API and leverage higher-level features for common data operations. The library is actively maintained and frequently updated, with a current version of 0.12.292 and requires Python 3.9 or greater.
Warnings
- gotcha API Key Configuration: Pycarlo requires an API Key ID (MCD_ID) and API Secret (MCD_TOKEN) for authentication. These are typically sourced from environment variables or a configuration file (~/.mcd/profiles.ini) created by the Monte Carlo CLI (`montecarlo configure`). Incorrect or missing credentials will prevent the SDK from functioning.
- gotcha Enum Backward Compatibility: Unlike `sgqlc`'s default behavior, Pycarlo is designed to be backward compatible with new enum values introduced in the Monte Carlo API. If the API returns an enum value not present in your SDK version, it will be returned as a string with a warning, instead of raising an error. While this prevents breakage, it means your local SDK might not fully reflect the latest API schema.
- gotcha Separation of Core and Features: Pycarlo is divided into `core` for direct API queries/mutations and `features` for higher-level convenience functions (e.g., circuit breakers, dbt, pii filtering). Ensure you import from the correct submodule (e.g., `from pycarlo.core import Client` vs. `from pycarlo.features.circuit_breakers import CircuitBreakerService`).
Install
-
pip install -U pycarlo
Imports
- Client
from pycarlo.core import Client
- Query
from pycarlo.core import Query
- Mutation
from pycarlo.core import Mutation
- Session
from pycarlo.core import Session
- CircuitBreakerService
from pycarlo.features.circuit_breakers import CircuitBreakerService
Quickstart
import os
from pycarlo.core import Client, Query, Session
# Configure session using environment variables or a named profile
# For production, it's recommended to set MCD_ID and MCD_TOKEN environment variables.
# Alternatively, you can configure via the Monte Carlo CLI: `montecarlo configure`
# If using a named profile, ensure ~/.mcd/profiles.ini is set up.
mcd_id = os.environ.get('MCD_ID', 'your_mcd_id_here')
mcd_token = os.environ.get('MCD_TOKEN', 'your_mcd_token_here')
# Instantiate a client. By default, it uses the 'default' profile or environment variables.
# You can also pass credentials explicitly or specify a profile:
# client = Client(Session(mcd_id=mcd_id, mcd_token=mcd_token))
client = Client()
# Create a query object
query = Query()
query.get_user.email()
# Execute the query
try:
response = client(query)
user_email = response.get_user.email
print(f"Current user email: {user_email}")
except Exception as e:
print(f"An error occurred: {e}")