Snowflake ML Python Library

1.34.0 · active · verified Mon Apr 13

The Snowflake ML Python Library (snowflake-ml-python) is the official client for interacting with Snowflake to build and deploy machine learning solutions. It provides interfaces for machine learning lifecycle management including session management, model development, model registry, feature store, and experiment tracking. The library is actively maintained with frequent minor releases, typically on a monthly to bi-monthly cadence, and is currently at version 1.34.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a Snowflake ML Session by first creating a Snowpark Session using environment variables for connection parameters. This session is the entry point for all operations within the snowflake-ml-python library.

import os
from snowflake.snowpark import Session
from snowflake.ml.session import get_session

# Ensure environment variables are set for connection:
# SNOWFLAKE_ACCOUNT, SNOWFLAKE_USER, SNOWFLAKE_PASSWORD, SNOWFLAKE_ROLE,
# SNOWFLAKE_WAREHOUSE, SNOWFLAKE_DATABASE, SNOWFLAKE_SCHEMA
connection_parameters = {
    "account": os.environ.get("SNOWFLAKE_ACCOUNT", "your_account"),
    "user": os.environ.get("SNOWFLAKE_USER", "your_user"),
    "password": os.environ.get("SNOWFLAKE_PASSWORD", "your_password"),
    "role": os.environ.get("SNOWFLAKE_ROLE", "your_role"),
    "warehouse": os.environ.get("SNOWFLAKE_WAREHOUSE", "your_warehouse"),
    "database": os.environ.get("SNOWFLAKE_DATABASE", "your_database"),
    "schema": os.environ.get("SNOWFLAKE_SCHEMA", "your_schema"),
}

session = None
try:
    # Establish a Snowpark Session
    session = Session.builder.configs(connection_parameters).create()
    print("Snowpark Session created successfully.")
    print(f"Current database: {session.get_current_database()}, schema: {session.get_current_schema()}")

    # Obtain the Snowflake ML Session (uses the Snowpark session)
    ml_session = get_session(session)
    print("Snowflake ML Session obtained successfully.")

    # You can now use ml_session for various ML tasks, e.g.,
    # ml_session.model.deploy(...)
    # ml_session.feature_store.register_feature_view(...)

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    if session:
        session.close()
        print("Snowpark Session closed.")

view raw JSON →