[Python] Central database connection script not working after using its function in other...

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página