1

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?

Tom
  • 22,301
  • 5
  • 63
  • 96

2 Answers2

16

The second assignement is misspelled.

You wrote connnection_info = LIVE_CONNECTION with 3 n's.

Van Gale
  • 43,536
  • 9
  • 71
  • 81
  • 1
    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 Apr 16 '09 at 01:54
  • 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 Apr 16 '09 at 11:21
4

Typo: connnection_info = LIVE_CONNECTION

RossFabricant
  • 12,364
  • 3
  • 41
  • 50