Snowflake Python APIs 教程的常用设置¶
简介
This topic provides instructions for the common setup required for all Snowflake Python APIs tutorials available in this documentation.
Overview of the Snowflake Python APIs¶
Before starting your setup, take a look at the Snowflake Python APIs structure. The following table lists some common modules in the API:
| Module | Description |
|---|---|
snowflake.core | Defines an iterator to represent certain resource instances fetched from the Snowflake database. |
snowflake.core.database | Manages Snowflake databases. |
snowflake.core.schema | Manages Snowflake schemas. |
snowflake.core.table | Manages Snowflake tables. |
snowflake.core.task | Manages Snowflake tasks. |
snowflake.core.task.dagv1 | A set of APIs at a higher level than the task APIs in snowflake.core.task to more conveniently manage task graphs (DAGs). |
snowflake.core.compute_pool | Manages compute pools in Snowpark Container Services. |
snowflake.core.image_repository | Manages image repositories in Snowpark Container Services. |
snowflake.core.service | Manages services in Snowpark Container Services. |
For a complete list of the APIs currently available, see the API reference documentation.
The snowflake.core module represents the entry point to the core Snowflake Python APIs that manage Snowflake objects. To use the
API, you follow a common pattern:
-
使用 Snowpark 或 Python Connector 连接建立会话,表示您与 Snowflake 的连接。
-
Import and instantiate the
Rootclass fromsnowflake.core, and pass the Snowpark session object as an argument.You use the resulting
Rootobject to access the rest of the methods and types in the API.
For more information about the programming model of the API, see Snowflake Python APIs: General concepts.
下面的代码提供了此模式的典型示例:
For more information about various connection options and attributes, see Connect to Snowflake with the Snowflake Python APIs.
Note
The Snowflake Python APIs can establish a connection to Snowflake using either a Snowpark session or a Python Connector connection. The preceding example uses a Snowpark session.
继续执行下一步,开始设置 API 及您的开发环境!
Install the Snowflake Python APIs¶
Important
The Snowflake Python APIs currently supports the following versions of Python:
Generally available versions:
- 3.9 (deprecated)
- 3.10
- 3.11
- 3.12
- 3.13
在安装 API 之前,您需要激活 Python 环境。
在本教程中,您可以使用 conda 或虚拟环境 (venv)。
-
To create and activate a conda or virtual environment, open a command-line terminal and run the following commands:
-
The Snowflake Python APIs package is available in PyPI.
若要在新的 conda 或虚拟环境中安装 API 包,运行以下命令:
-
To install the
snowflake-snowpark-pythonpackage, run the following command:In these tutorials, you use the
snowflake.snowpark.Sessionobject from the Snowpark API for Python to create a connection to Snowflake.
设置开发环境
This tutorial walks through code examples that you can run in a Jupyter notebook. Each step in the tutorial incrementally showcases the capabilities of the Snowflake Python APIs.
首先设置您的开发环境,以便在笔记本中运行代码示例。
-
Create a file named
$HOME/.snowflake/connections.tomlwith the following connection parameters, and update it with your real credentials:Note
The
accountparameter does not support account identifiers with underscores. You must specify an account identifier with dashes in place of any underscores. For more information, see Account name in your organization.This example specifies these parameters as the default connection to Snowflake in your environment by creating a connection definition named
default. -
使用如下方法之一打开笔记本:
-
在支持 Jupyter 笔记本的代码编辑器(例如 Visual Studio Code)中打开一个新笔记本。
-
To open a notebook in your browser, start a notebook server with the command
jupyter notebook.To ensure that your environment can run a notebook, run
conda install notebookin your terminal before starting the notebook server.
-
-
在笔记本的第一个单元格中,运行以下导入语句:
Note
After running this cell, you might be prompted to set your Python kernel. If you activated a conda environment, select conda as the Python kernel (for example, something similar to:
~/miniconda3/envs/<your conda env>/bin/python).
在此单元格中,导入 Snowpark 和管理 Snowflake 对象的核心 APIs。
-
要与 Snowflake 建立连接,请在下一个单元格中,运行以下代码:
In this cell, you create a Snowpark session and set the connection parameters for your session by specifying the
connection_nameas thedefaultconnection definition that you previously configured. -
To create a
Rootobject, pass yoursessionobject to theRootconstructor:
And that’s it! By running the code in these three cells, you’re now ready to use the Snowflake Python APIs.
下一步是什么?
You can now explore Tutorial 1: Create a database, schema, table, and warehouse.