0
0
Snowflakecloud~10 mins

Snowpark for Python basics in Snowflake - Commands & Configuration

Choose your learning style9 modes available
Introduction
Snowpark for Python lets you write Python code that runs directly inside Snowflake's data platform. This helps you process data close to where it lives, making data tasks faster and simpler without moving data around.
When you want to run Python data transformations directly inside Snowflake without exporting data.
When you need to build data pipelines that combine SQL and Python logic seamlessly.
When you want to use Python libraries for data processing but keep data secure inside Snowflake.
When you want to avoid managing separate compute resources for Python data jobs.
When you want to write reusable Python functions that run close to your data.
Commands
This command installs the Snowpark Python library so you can write Python code that connects to Snowflake and runs data operations.
Terminal
pip install snowflake-snowpark-python
Expected OutputExpected
Collecting snowflake-snowpark-python Downloading snowflake_snowpark_python-1.5.0-py3-none-any.whl (123 kB) Installing collected packages: snowflake-snowpark-python Successfully installed snowflake-snowpark-python-1.5.0
Runs a Python script that connects to Snowflake, creates a DataFrame, and shows data. This demonstrates basic Snowpark usage.
Terminal
python snowpark_example.py
Expected OutputExpected
ID NAME 0 1 Alice 1 2 Bob 2 3 Carol
Key Concept

If you remember nothing else from Snowpark for Python, remember: it lets you run Python code directly inside Snowflake to process data efficiently without moving it.

Code Example
Snowflake
from snowflake.snowpark import Session

connection_parameters = {
    "account": "xy12345.us-east-1",
    "user": "MY_USER",
    "password": "MY_PASSWORD",
    "role": "SYSADMIN",
    "warehouse": "COMPUTE_WH",
    "database": "MY_DB",
    "schema": "PUBLIC"
}

session = Session.builder.configs(connection_parameters).create()

# Create a simple DataFrame
data = [(1, "Alice"), (2, "Bob"), (3, "Carol")]
df = session.create_dataframe(data, schema=["ID", "NAME"])

# Show the data
pandas_df = df.to_pandas()
print(pandas_df)

session.close()
OutputSuccess
Common Mistakes
Not installing the snowflake-snowpark-python package before running code.
The Python code will fail because the Snowpark library is missing.
Always run 'pip install snowflake-snowpark-python' first to install the required library.
Trying to run Snowpark code without setting up Snowflake connection parameters.
The code cannot connect to Snowflake and will error out.
Provide correct account, user, password, and warehouse details in the connection code.
Summary
Install the Snowpark Python library to enable Python data processing inside Snowflake.
Write Python scripts that connect to Snowflake using connection parameters.
Create DataFrames in Python and run operations that execute inside Snowflake.
Use to_pandas() to bring small results back to Python for viewing or further use.
Close the session to clean up the connection after your work is done.