Mohan Stocks
Mohan Stocks

Reputation: 23

TypeError when creating a Snowflake session using Snowpark and Snowflake Connector

I'm trying to create a Snowflake session using Snowpark and Snowflake Connector in Python. However, I'm encountering a TypeError with the following error message:

TypeError: config() missing 1 required positional argument: 'value'

Here's the relevant code snippet:

import pandas as pd
import boto3 as ab
from snowflake.snowpark.session import Session
import snowflake.connector as sc
import os
from configparser import ConfigParser

config = ConfigParser()
config.read(os.path.dirname(__file__) + str("/DBConn/config.ini"))
cs = config.sections()

connection_parameters = {
    "account": config.get(cs[0], 'account'),
    "user": config.get(cs[0], 'user'),
    "password": config.get(cs[0], 'password'),
    "role": config.get(cs[0], 'defaultrole'),  # optional
    "warehouse": config.get(cs[0], 'defaultwarehouse'),  # optional
    "database": config.get(cs[0], 'defaultdatabase'),  # optional
    "schema": config.get(cs[0], 'defaultschema')  # optional
}

sfSession = Session.builder.config(connection_parameters).create()
print(sfSession)
con = sc.connect(**connection_parameters)
print(con)

I have verified that the configuration parameters are correctly read from the config.ini file. However, when calling the config() method on the Session.builder, it throws a TypeError. It seems like the config() method is expecting an additional positional argument, but I'm unsure what it should be.

I would greatly appreciate any insights or suggestions on how to resolve this issue. Thank you in advance for your help!

Reference URL of Config Parameter from snowflake documentation https://docs.snowflake.com/en/developer-guide/snowpark/python/creating-session

Thanks.

Expecting to establish the session from snowflake using snowpark

Upvotes: 2

Views: 990

Answers (3)

neeru0303
neeru0303

Reputation: 179

If you are using dict object for snowpark session creation, Use .configs method instead of .config method in Session.builder.

https://docs.snowflake.com/en/developer-guide/snowpark/python/creating-session#connect-by-specifying-connection-parameters

To create the session:

  1. Create a Python dictionary (dict) containing the names and values of the parameters for connecting to Snowflake.

  2. Pass this dictionary to the Session.builder.configs method to return a builder object that has these connection parameters.

  3. Call the create method of the builder to establish the session.


from snowflake.snowpark.session import Session
import os
from configparser import ConfigParser

config = ConfigParser()
config.read(os.path.dirname(__file__) + str("/DBConn/config.ini"))
cs = config.sections()

connection_parameters = {
    "account": config.get(cs[0], 'account'),
    "user": config.get(cs[0], 'user'),
    "password": config.get(cs[0], 'password'),
    "role": config.get(cs[0], 'defaultrole'),  # optional
    "warehouse": config.get(cs[0], 'defaultwarehouse'),  # optional
    "database": config.get(cs[0], 'defaultdatabase'),  # optional
    "schema": config.get(cs[0], 'defaultschema')  # optional
}

sfSession = Session.builder.configs(connection_parameters).create()
print(sfSession)


Upvotes: 0

Nicolas Sanchez
Nicolas Sanchez

Reputation: 57

Please ensure you are using the latest snowpark release and that you are passing a dictionary with key-value pairs (both must be strings!!)

The Session.builder.config should recognize a dictionary passed as unique argument as stated in the documentation:

To create the session:

  1. Create a Python dictionary (dict) containing the names and values of the parameters for connecting to Snowflake.
  2. Pass this dictionary to the Session.builder.configs method to return a builder object that has these connection parameters.
  3. Call the create method of the builder to establish the session.

https://docs.snowflake.com/en/developer-guide/snowpark/python/creating-session

Upvotes: 0

Saxtheowl
Saxtheowl

Reputation: 4658

config() is expecting key-value pairs, not a dictionary, just modify your code to call config() once for each key-value pair, rather than passing it a dictionary.

sessionBuilder = Session.builder

for key, value in connection_parameters.items():
    sessionBuilder.config(key, value)

sfSession = sessionBuilder.create()

Upvotes: 1

Related Questions