Run Python scripts in Snowflake¶
This topic walks you through developing, deploying, and scheduling Python scripts in Snowflake. You write and test your code locally, then deploy it as a Notebook Project Object — a schema-level object that runs your script non-interactively on a warehouse or compute pool. Once deployed, you can run your project on demand or schedule it with a task.
Prerequisites¶
To complete this guide, you need the following on your development machine:
Python 3.10, 3.11, or 3.12
The Snowflake CLI. If you haven’t used the Snowflake CLI before, run snow connection add to connect the CLI to your Snowflake account.
Set up your project¶
Clone the sample Python project:
Create a virtual environment and install the dependencies:
The template project contains the following files:
src/main.py— the main entry point for your scriptsrc/args_example.py— shows how to pass and process command-line argumentssrc/udf_example.py— shows how to register and reference UDFsrequirements-prod.txt— production dependencies (installed at run time in Snowflake)requirements-dev.txt— development dependencies, including the Snowflake CLIsnow_app.yml— configuration for runtime, compute, and dependenciestests/— a sample test suite
Develop and test locally¶
Run the demo script locally to verify your environment:
Run the test suite:
You can extend the sample scripts to read or write data specific to your team or use case. Iterate locally until your script is ready, then configure and deploy it to Snowflake.
Configure your project¶
Before you deploy, configure runtime and dependency settings in the snow_app.yml file
at the root of your project. This file sets default values so you don’t have to specify them every
time you run the project.
runtime¶
Use the runtime property to specify the runtime version, compute platform, and other
properties for running your project.
runtime.version¶
Required. The Container Runtime version to run the project on.
Allowed values:
python_3.10python_3.11python_3.12
For the full list of allowed values, see EXECUTE NOTEBOOK PROJECT.
runtime.compute_pool¶
The compute pool to run the project on. If not specified, the project runs on the active warehouse of the session.
runtime.query_warehouse¶
The warehouse to use for SQL queries when a runtime.compute_pool is specified. If not
specified, queries run on the active warehouse of the session.
dependencies¶
A reference to the requirements.txt file that lists the Python dependencies for the project.
Dependencies are installed from the built-in snowflake.snowpark.pypi_shared_repository PyPI
repository.
If you split production and development dependencies, specify your production dependencies:
Deploy to Snowflake¶
Deploy your project to Snowflake so it can be run non-interactively.
Create a stage to hold the project files:
Copy the project contents to the stage:
Create the Notebook Project Object:
The project is now deployed to your Snowflake account.
Run your project¶
You can run a deployed project using the Snowflake CLI or SQL.
Snowflake CLI¶
SQL¶
If you provide COMPUTE_POOL, QUERY_WAREHOUSE, RUNTIME, or
EXTERNAL_ACCESS_INTEGRATIONS in the command, those values override the defaults set in
snow_app.yml.
Schedule recurring runs¶
To run your project on a recurring schedule, wrap the run command in a Snowflake Task:
After creating the task, resume it to start the schedule:
Automate deployment with CI/CD¶
You can run the deploy commands in a CI/CD pipeline to automatically deploy your project when
you merge changes. The following example shows a GitHub Actions workflow that deploys on push to
the main branch:
Adapt this pattern to your CI system by running snow stage copy followed by
snow notebook project create --overwrite after authenticating the Snowflake CLI.
Register UDFs from your project¶
If your script depends on User Defined Functions and you want to manage their lifecycle through
the project, create them during the script’s run with session.udf.register or the
@udf decorator. See src/udf_example.py in the sample project for an example.
Reference¶
SQL commands¶
CREATE NOTEBOOK PROJECT¶
Create a project from a stage:
Create a project from a workspace:
For full syntax details, see CREATE NOTEBOOK PROJECT.
EXECUTE NOTEBOOK PROJECT¶
For full syntax details, see EXECUTE NOTEBOOK PROJECT.
Snowflake CLI commands¶
snow notebook project create¶
snow notebook project execute¶
snow notebook project drop¶
snow notebook project list¶
Limitations¶
The following limitations apply:
.ipynbnotebooks can only run on compute pools, not warehouses..pyfiles can run on either.Java and Scala are not supported.
Event table logging is not available when running on warehouses.
Only
requirements.txtis supported for specifying dependencies.pyproject.tomlis not supported.