How to connect and perform operations of Snowflake using Python

Lets see power of Python
SNOWFLAKE Data Warehouse

Install Snowflake connector to use in Python

python -m pip install --upgrade pip

NOTE: Below instructions can be followed anytime you install any python library

If you are using Jupyter Notebook/Jupyter Lab/Spyder from Anaconda Distribution then run above command in Anaconda prompt by going in START menu of your computer and search for Anaconda prompt.

Else, if you are using python directly then use command prompt to verify above.

pip install --upgrade snowflake-connector-python

Make connection with Snowflake using Python

import snowflake.connector as sf
import pandas as pd
user='snowflake-username'password='snowflake-password'account='unique-acct-id.region' ### This can be found in your snowflake account URL. Example: (if you are using Google cloud) or (if using AWS cloud)database='database-name'warehouse='virtual-warehouse-name'schema = 'schema-name'role = 'role' ### Choose role which have access to above mentioned database, warehouse and schema, ACCOUNTADMIN is the highest level role in snowflake# Establishing connection with snowflake
conn = sf.connect(user = user,
password = password,
account = account,
snowflakecursor = conn.cursor()# user defined function to run sql queries
def run_query(connection,query):
cursor = connection.cursor()
# This try and except block will help to pass user parameters on snowflake
sql = 'use warehouse {}'.format(warehouse)
run_query(conn, sql)

# if virtual warehouse is in suspended state, this try/except block will resume the warehouse
sql = 'alter warehouse {} resume'.format(warehouse)
run_query(conn, sql)

sql = 'use database {}'.format(database)
run_query(conn, sql)

sql = 'use role {}'.format(role)
run_query(conn, sql)

sql = 'use schema {}'.format(schema)
run_query(conn, sql)
# in case of any error, an exception will be raised
except Exception as e:

Using above script, your connection will be established and required user parameters such as database, schema, role and virtual warehouse selection will be made on snowflake.

NOTE: While copying above script, INDENTATIONS could be disturbed so just focus on indentations if you get any errors.

Perform operations by running SQL statements on Python

sql = "create or replace table employee(id number(10), \
name varchar(20) \
department varchar(8))"
run_query(conn, sql)

a.) sql variable will be created which contains the sql statement to create employee table.
b.) at the end of 1st and 2nd line of sql statement, ‘\’ is used to tell python that the next line is in continuation which is why ‘\’ is not in the last line of sql statement
c.) run_query is a user defined function created in the previous section while establishing connection with snowflake. It requires two parameters: ‘conn’ which is the connection string of snowflake and other one is ‘sql’ which we have written above.

Store results of query in a Pandas DataFrame

import pandas as pdsql = "select * from employee limit 10"df = pd.read_sql(sql, conn)

~we have imported pandas module
~’sql’ string contains sql statement to fetch first 10 rows (using LIMIT keyword) from employee table
~using snowflake connection string ‘conn’, we are running ‘sql’ on snowflake and storing results in Pandas DataFrame ‘df’



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store