I have a bit of Python to connect to a database with a switch throw in for local versus live.
LOCAL_CONNECTION = {'server': '127.0.0.1', 'user': 'root', 'password': '', 'database': 'testing'} LIVE_CONNECTION = {'server': '10.1.1.1', 'user': 'x', 'password': 'y', 'database': 'nottesting'} if debug_mode: connection_info = LOCAL_CONNECTION else: connnection_info = LIVE_CONNECTION self.connection = MySQLdb.connect(host = connection_info['server'], user = connection_info['user'], passwd = connection_info['password'], db = connection_info['database'])
Works fine locally (Windows, Python 2.5) but live (Linux, Python 2.4) I’m getting:
UnboundLocalError: local variable 'connection_info' referenced before assignment
I see the same error even if I remove the if/ else and just assign connection info directly to the LIVE_CONNECTION value. If I hard-code the live connection values into the last line, it all works. Clearly I’m sleepy. What am I not seeing?
The second assignement is misspelled.
You wrote
connnection_info = LIVE_CONNECTIONwith 3 n’s.