How to use Snowflake Python Connector and Example

  • Post author:
  • Post last modified:June 16, 2021
  • Post category:Snowflake
  • Reading time:6 mins read

Snowflake is a cloud data warehouse environment and provides support for many major programming languages that uses JDBC or ODBC drivers. You can use Snowflake provided connector to connect your application. In this article, we will check one of the such connectors. i.e Python connector for Snowflake.

How to use Snowflake Python Connector and Example

Snowflake Python Connector

The Snowflake Connector for Python provides an interface for developing Python applications that can connect to cloud data warehouse and perform all standard operations. 

The connector is a pure python package that can be used to connect your application to the cloud data warehouse. The connector supports all standard operations. For example, query execution, loading, accessing data from external source (S3), and many more.

Install Snowflake Python Connector

You can install connector by using pip command or conda installer. There are many ways you can install connector, but, pip and conda installer are easiest as the package is installed along with dependent packages.

For example,

pip command

pip install snowflake-connector-python

Conda command

Snowflake connector is not available on standard conda libraries. You have to use conda-forge channel to install using conda installer.

conda install -c conda-forge snowflake-connector-python

Snowflake Connector Login Parameters

Following example provides creating connection object by providing login parameters.

conn = snowflake.connector.connect(
                user='USER',
                password='PASSWORD',
                account='ACCOUNT',
                warehouse='WAREHOUSE',
                database='DATABASE',
                schema='SCHEMA'
                )

where, user, password and account are mandatory parameters. However, other parameters are optional and Snowflake uses default values.

You can also read the credentials from the Windows or Linux environment variables.

For example, use below syntax to read environment variable in Python.

PASSWORD = os.getenv('SNOWSQL_PWD')

And use PASSWORD in your login parameter.

Related Article

Snowflake Python Connector Example

Firstly, it is very easy to use the Python connector in your application. You just have to set the login parameters with required credential details and you are good to go.

Following example demonstrates the usage of python connector to get current date.

import snowflake.connector

# Connectio string
conn = snowflake.connector.connect(
                user='snuser',
                password='password@123',
                account='xyz12345.us-east-2',
                #warehouse='COMPUTE_WH',
                database='DEMO_DB',
                schema='public'
                )

# Create cursor
cur = conn.cursor()

# Execute SQL statement
cur.execute("select current_date;")

# Fetch result
print cur.fetchone()[0]

Output:

You will get following output when you execute Python progam.

2019-12-13

Related Articles

Hope this helps 🙂