tags:

views:

198

answers:

2

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?

+14  A: 

The second assignement is misspelled.

You wrote connnection_info = LIVE_CONNECTION with 3 n's.

Van Gale
3 n's are hard to see!
S.Lott
Yep! Even when I saw the '=' didn't line up it took a third scan for me to find the extra n. Anyway, hopefully Tom's next question isn't about static typing :)
Van Gale
Wow, I am a yutz. You'd think the whitespace of Python would have made it obvious to me when that = didn't line up. Thanks to everyone.
Tom
+4  A: 

Typo: connnection_info = LIVE_CONNECTION

RossFabricant