question

chandrasekar.b03_190734 avatar image
chandrasekar.b03_190734 asked chandrasekar.b03_190734 commented

Unable to solve "Connection not defined" error on python Interpreter

I had connected Cassandra on gitpod successfully(no issues) but I was unable to do it on python Interpreter. It is sometime (I don't know how) connecting to the cluster but not always rather prompting "connection not defined". I have tried all the ways. I have attached a screenshot of my window for reference.

workshoppython driver
db-connection2.jpg (494.6 KiB)
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

bettina.swynnerton avatar image
bettina.swynnerton answered chandrasekar.b03_190734 commented

Hi @chandrasekar.b03_190734,

could you check your full path for the connection bundle in the db_connection.py file? And check that it matches the file location?

You can test by placing the secure connection bundle directly into the same directory where you run the python shell. Then you only need to specify the filename itself:

For example, mine looks like this:

# This is the Zip file you downloaded
SECURE_CONNECT_BUNDLE = 'secure-connect-killrvideocluster.zip'
# This is the username, recommended value was KVUser
USERNAME = "KVUser"
# This is the password, recommended value was KVPassword
PASSWORD = "KVPassword"
# This is the keyspace name, recommended value was killrvideo
KEYSPACE = "killrvideo"

Running Ex02_Connect_to_Cassandra.py from the interpreter like this then works ok in my case:

$ python
Python 3.7.4 (default, Jul 12 2019, 10:18:18) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exec(open("Ex02_Connect_to_Cassandra.py").read())
========================================
Start exercise
Your are now connected to cluster caas-cluster
Success
Closing connection (up to 10s)
======================================

See if this gets you on the right track. Thanks for your patience with this.


Edit: Please make the following change in the except block in the python code for Ex02_Connect_to_Cassandra.py:

except Exception as e:
    print(e)
    print('Failure')

This will then tell us if it is a file not found or an authentication error.


Update: If you are seeing timeout errors in the connection, please increase the connection timeout in the db_connection.py where we setup the connection to Astra:

class Connection:
    def __init__(self):
        self.secure_connect_bundle=SECURE_CONNECT_BUNDLE
        self.path_to_creds=''
        self.cluster = Cluster(
            cloud={
                'secure_connect_bundle': self.secure_connect_bundle,
                'connect_timeout': 10  # Default is 5 seconds
            },
            auth_provider=PlainTextAuthProvider(USERNAME, PASSWORD)
        )
        self.session = self.cluster.connect(KEYSPACE)
    def close(self):
        self.cluster.shutdown()
        self.session.shutdown()


4 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

chandrasekar.b03_190734 avatar image chandrasekar.b03_190734 commented ·

hi @bettina.swynnerton ,

i have tried that also but the error still persists. I don't know how i'm gonna handle this error. I have also attached a screenshot of what i did as you have told for your reference. kindly help me solve this issue :).

0 Likes 0 ·
db4.jpg (402.5 KiB)
bettina.swynnerton avatar image bettina.swynnerton ♦♦ chandrasekar.b03_190734 commented ·

ok, let's see if we can get to the source of the problem.

Would you try this in your interpreter?

>>> import os
>>> os.path.isfile('secure-connect-killrvideocluster.zip')
True
>>>

This indicates the file is in the working directory.

0 Likes 0 ·
bettina.swynnerton avatar image bettina.swynnerton ♦♦ chandrasekar.b03_190734 commented ·

Hi @chandrasekar.b03_190734,

please see my update in my answer about the connection timeout settings in the db_connection.py file, would you try this and let us know if this works?

0 Likes 0 ·
chandrasekar.b03_190734 avatar image chandrasekar.b03_190734 bettina.swynnerton ♦♦ commented ·

I'm unable to reply to you on this community. I have replied you on discord

0 Likes 0 ·