Reputation: 1
I am new to Python and I have been working on a project which need to run some queries from Snowflake database. My approach here is to create a central .py script with a function connect_snowflake() that can be called in other scripts. The reason I am doing this is that once I have done my development on DEV environment, I don't want to go on changing credentials for TEST environment in multiple files.
Below is my Snowflake connection script named as pySnowflake_Connection.py,
import snowflake.connector
def connect_snowflake(user, password, account, warehouse, database, schema):
conn = None # Initialize connection variable
try:
conn = snowflake.connector.connect(
user=user,
password=password,
account=account,
warehouse=warehouse,
database=database,
schema=schema
)
print("✅ Connection to Snowflake successful!")
# Execute a simple query to verify connection
cur = conn.cursor()
cur.execute("SELECT CURRENT_VERSION()")
version = cur.fetchone()
print(f"🔹 Snowflake Version: {version[0]}")
except Exception as e:
print(f"❌ Failed to connect to Snowflake: {e}")
# Replace with actual credentials
connect_snowflake(
user="USER_DEV",
password="xxxxxxxxxx",
account="xxxxxxx.north-europe.azure",
warehouse="xxxxxxxx_DEV_WH",
database="xxxxxxxxx_DEV_DB",
schema="xxxxxxxxxxxx"
)
Now that I have written this script it runs successfully individually & gives me output as below
✅ Connection to Snowflake successful!
� Snowflake Version: 9.3.3
Now here is my second script which is trying to execute a query to fetch some records from a table in Snowflake DB. It runs to success on connection but asks for credentials again. This is defeating the purpose of creating a central connection.
Below is my second script,
import snowflake.connector
from snowflake.connector import connect
import pandas as pd
from pySnowflake_Connection import connect_snowflake
connsnf = connect_snowflake()
table_query="select * from TABLE_DATA_STG"
#pd.read_sql_query(table_query,snowflake_connection)
connsnf.execute(table_query)
datafetch = connsnf.fetch_pandas_all()
#print(datafetch.to_string())
datafetch.to_csv('C:/xxxx/Project/PyDataLoad/TABLE_DATA_STG.csv',sep = '|',encoding='utf-8',doublequote=True)
When doing this, it still gives me output like below,
✅ Connection to Snowflake successful!
� Snowflake Version: 9.3.3
Traceback (most recent call last):
File "c:\xxxx\Project\PyDataLoad\pyImport_SNF_Data_2.py", line 6, in <module>
connsnf = connect_snowflake()
^^^^^^^^^^^^^^^^^^^
TypeError: connect_snowflake() missing 6 required positional arguments: 'user', 'password', 'account', 'warehouse', 'database', and 'schema'
The table has some 10,000 records & thus I am expecting the actual number. Once successful, I will be running simple select col1, col2... from STG_TABLE_1 queries. But it seems I am not getting output for this first query.
I wrote another script where snowflake.connector, its credentials & all are part of same script. This script with everything in one place works. But as said before, I want a centralised working script as I am going to write more such scripts & when migrating them over to new environment, I don't want to keep editing multiple files. Much hard it would be if I have to transfer my code to someone else & then need to keep making changes across multiple files.
I even tried the. env method, but with it didn't even connect to Snowflake.
Upvotes: -3
Views: 24
Reputation: 142985
You defined function as
def connect_snowflake(user, password, account, warehouse, database, schema)
so now you have to always execute it with all parameters
connsnf = connect_snowflake(user="USER_DEV", ..., ...)
But function would need also return conn
to assign it to connsnf = ...
def connect_snowflake(user, password, account, warehouse, database, schema):
# ... code ...
return conn
If you don't want to repeate parameters then in pySnowflake_Connection
put it in function without parameters
def default_connection():
return connect_snowflake(
user="USER_DEV",
password="xxxxxxxxxx",
account="xxxxxxx.north-europe.azure",
warehouse="xxxxxxxx_DEV_WH",
database="xxxxxxxxx_DEV_DB",
schema="xxxxxxxxxxxx"
)
And later use
from pySnowflake_Connection import default_connection
connsnf = default_connection()
If you plan to use conn
in many files in the same project then better assign connection to global variable in pySnowflake_Connection
conn = connect_snowflake(
user="USER_DEV",
password="xxxxxxxxxx",
account="xxxxxxx.north-europe.azure",
warehouse="xxxxxxxx_DEV_WH",
database="xxxxxxxxx_DEV_DB",
schema="xxxxxxxxxxxx"
)
and later import only this variable
from pySnowflake_Connection import conn
Upvotes: 0
Reputation: 139
you are simply not passing it the required args when calling connect_snowflake
connsnf = connect_snowflake()
Upvotes: 0