Snowflake Python API: Managing Snowflake objects with Python

The Snowflake Python API is a unified library that seamlessly connects Python with Snowflake workloads. It is intended to provide comprehensive APIs for interacting with core Snowflake resources across data engineering, Snowpark, and application workloads without using SQL commands.

You can use the Snowflake Python API to manage Snowflake resources by creating, deleting, or modifying them, and more. You can use Python to perform tasks you might otherwise perform with Snowflake SQL commands.

With the Snowflake Python API, you can currently manage the following Snowflake objects:

For an introductory tutorial on using the Snowflake Python API, see Quickstart: Getting Started with the Snowflake Python API.

Example

Code in the following example executes a task called my_task. The code uses connection parameters defined in a configuration file to create a connection to Snowflake. Using the resulting Session object, the code creates a Root object to use the API’s types and methods. Using the API, the code creates a Task object that represents my_task. Using a TaskResource object called task_res, the code executes the task.

from snowflake.core import Root
from snowflake.core.task import Task
from snowflake.snowpark import Session

session = Session.builder.config("connection_name", "myconnection").create()
root = Root(session)

tasks = root.databases["mydb"].schemas["myschema"].tasks
task_res = tasks['my_task']

task_res.execute()
Copy

For more information about task-related actions you can perform with the API, see Managing Snowflake tasks and task graphs with Python.

Limitations

The Snowflake Python API is not currently available in the Snowflake Anaconda channel.

Supported versions

The supported versions of Python are:

  • 3.8

  • 3.9

  • 3.10

  • 3.11

Developer guides

Guide

Description

Install the Snowflake Python API library

Install the Snowflake Python API package.

Connect to Snowflake with the Snowflake Python API

Connect to Snowflake from Python code.

Managing Snowflake tasks and task graphs with Python

Use the API to create, execute, and manage tasks and task graphs.

Managing Snowflake databases, schemas, and tables with Python

Use the API to create and manage databases, schemas, and tables.

Managing Snowflake virtual warehouses with Python

Use the API to create and manage virtual warehouses.

Managing Snowpark Container Services with Python

Use the API to manage components of Snowpark Container Services, including compute pools, image repositories, and services.

References

Snowflake Python API Reference

Costs of Snowflake access

To reduce costs—–for both usage credit and network activity—–the Snowflake Python API is designed to communicate with Snowflake only when you call methods designed to synchronize with Snowflake.

Objects in the API are either local references (or handles) or snapshots of state stored on Snowflake. In general, when you process information that was retrieved from Snowflake, you do so through a local, in-memory reference object.

These references do not synchronize with Snowflake until you call a method. When you call a method, you are usually incurring costs in both usage credit and network activity. In contrast, when you work with in-memory references, such as when accessing attributes, your work is performed locally and incurs no such costs.