Snowflake Python API for Resource Management (snowflake-core)
The `snowflake-core` library is a Python API that provides programmatic access to Snowflake entity metadata and allows for interaction with Snowflake resources. It enables users to create, delete, and modify Snowflake objects using Python, offering similar functionality to Snowflake's SQL command API. The library is currently at version 1.12.0 and is actively maintained with regular releases.
Warnings
- gotcha Always verify you are consulting the documentation for the latest version of `snowflake-core`. Snowflake's documentation portal might default to older versions, which can lead to confusion regarding available features or API signatures.
- gotcha Understand the distinction between local Python objects (references) and actual Snowflake snapshots. Operations that modify or retrieve information from Snowflake generally require explicit calls through the `Root` object and its collections, as Python objects in memory don't automatically sync with the remote state.
- gotcha Connection parameters are typically sensitive and should be managed securely, preferably using environment variables as shown in official examples. Hardcoding credentials directly in code is discouraged.
Install
-
pip install snowflake-core
Imports
- Root
from snowflake.core import Root
- connect
from snowflake.connector import connect
- Session
from snowflake.snowpark import Session
Quickstart
import os
from snowflake.connector import connect
from snowflake.snowpark import Session
from snowflake.core import Root
# Option 1: Create Root instance from Snowflake Connection
connection_params = {
"account": os.environ.get("SNOWFLAKE_ACCOUNT", ""),
"user": os.environ.get("SNOWFLAKE_USER", ""),
"password": os.environ.get("SNOWFLAKE_PASSWORD", ""),
"database": os.environ.get("SNOWFLAKE_DATABASE", "test_db"),
"warehouse": os.environ.get("SNOWFLAKE_WAREHOUSE", "test_wh"),
"schema": os.environ.get("SNOWFLAKE_SCHEMA", "public"),
}
# Ensure required environment variables are set
if not all(connection_params.values()):
print("Warning: Some Snowflake connection parameters are missing. Please set SNOWFLAKE_ACCOUNT, SNOWFLAKE_USER, and SNOWFLAKE_PASSWORD environment variables.")
# Exit or handle error if connection is critical
try:
# Using snowflake.connector
connection = connect(**connection_params)
root_from_conn = Root(connection)
print(f"Root instance created from SnowflakeConnection: {root_from_conn}")
# Example: Accessing a database collection
# Assuming 'mydatabase' exists or will be created
# my_database = root_from_conn.databases["MYDATABASE"]
# print(f"Accessed database: {my_database}")
connection.close()
# Option 2: Create Root instance from Snowpark Session
# Ensure required environment variables are set for Snowpark as well if different
session = Session.builder.configs(connection_params).create()
root_from_session = Root(session)
print(f"Root instance created from Snowpark Session: {root_from_session}")
# Example: Accessing a compute pool collection
# compute_pools = root_from_session.compute_pools
# print(f"Accessed compute pools collection: {compute_pools}")
session.close()
except Exception as e:
print(f"An error occurred during quickstart: {e}")