brad
brad

Reputation: 61

python2.6 with MySQLdb, NameError 'MySQLdb' not defined

from the interpreter i can issue >>> from MySQLdb just fine. so, I'm assuming the module did actually load. My source looks as follows:

from Tkinter import *
from MySQLdb import *
"""
Inventory control for Affordable Towing

Functions:
connection() - Controls database connection
delete() - Remove item from database
edit() - Edit item's attributes in database
lookup() - Lookup an item
new() - Add a new item to database
receive() - Increase quantity of item in database
remove() - Decrease quantity of item in database
report() - Display inventory activity
transfer() - Remove item from one location, receive item in another

"""
def control():
....dbInfo = { 'username':'livetaor_atowtw', 'password':'spam', \
....'server':'eggs.com', 'base':'livetaor_towing', 'table':'inventory' }
....def testConnection():
........sql = MySQLdb.connect(user=dbInfo[username], passwd=dbInfo[password], \
........host=dbInfo[server], db=dbInfo[base])
........MySQLdb.mysql_info(sql)

....testConnection()

control()

this gives me:

brad@brads-debian:~/python/towing/inventory$ python inventory.py
Traceback (most recent call last):
..File "inventory.py", line 53, in
....control()
..File "inventory.py", line 26, in control
....testConnection()
..File "inventory.py", line 22, in testConnection
....sql = MySQLdb.connect(user=dbInfo[username], passwd=dbInfo[password], \
NameError: global name 'MySQLdb' is not defined

1) where am I going wrong?
2) any other gotcha's that you folks see?
3) any advice on how to check for a valid connection to the database, (not just the server)?

Upvotes: 0

Views: 7955

Answers (2)

Kurt Telep
Kurt Telep

Reputation: 729

This is due to the way you're importing the module and then referencing it.

Change:

from MySQLdb import *

to

import MySQLdb

if you plan on referencing it the fashion that you are.

Anyway, here’s how these statements and functions work:

From: http://effbot.org/zone/import-confusion.htm

import X imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can use X.name to refer to things defined in module X.

from X import * imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so X.name doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.

from X import a, b, c imports the module X, and creates references in the current namespace to the given objects. Or in other words, you can now use a and b and c in your program.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

from MySQLdb import * and import MySQLdb do very different things.

Upvotes: 1

Related Questions