Snowflake Connector ModuleNotFound only on Streamlit app

I’m trying to connect to our snowflake instance and print rows from a table in Streamlit. When I run the code locally the query executes and I can view the results in rows. However, the main Streamlit window shows a ModuleNotFoundError.

streamlit_app.py

import streamlit as st
import snowflake.connector

Initialize connection.

Uses st.experimental_singleton to only run once.

@st.experimental_singleton
def init_connection():
return snowflake.connector.connect(user = “XXX”,
authenticator=‘externalbrowser’,
account = “XXX”,
warehouse = “XXX”,
database = “XXX”,
schema = “XXX”)

conn = init_connection()

Perform query.

Uses st.experimental_memo to only rerun when the query changes or after 10 min.

@st.experimental_memo(ttl=600)
def run_query(query):
with conn.cursor() as cur:
cur.execute(query)
return cur.fetchall()

rows = run_query(“SELECT * from XXX;”)

Print results.

for row in rows:
st.write(f"{row[0]} has a :{row[1]}:")

Hi @jcscheide,

Thanks for posting!

Have you added “snowflake-connector-python” to your requirements.txt file?

Caroline :balloon:

Hi @Caroline, I was able to troubleshoot a tricky error!

In the virtual environment that streamlit was running in, I had installed both snowflake-connector-python and a package called snowflake. These two packages don’t work together (I don’t actually know what the snowflake package does), so uninstalling the snowflake package made snowflake-connector-python work!

2 Likes

Glad you figured out the solution, @jcscheide. In case you intend to deploy your app on Streamlit Community Cloud, here’s how you can install the connector on Cloud: Connect Streamlit to Snowflake - Streamlit Docs

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.