2011-04-10 2 views
0

인터프리터에서 >>>를 MySQLdb에서 정상적으로 실행할 수 있습니다. 그래서 모듈이 실제로로드되었다고 가정합니다.Python2.6 with MySQLdb, NameError 'MySQLdb'정의되지 않았습니다.

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()

이 나에게 제공합니다 :) 여기서 내가 잘못 가고

[email protected]:~/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

한 다음 내 소스 보인다?
2) 다른 사람이 본 사람이 있습니까?
3) 서버뿐만 아니라 데이터베이스에 대한 유효한 연결을 확인하는 방법에 대한 조언이 있습니까?

답변

2

이것은 모듈을 가져 와서 참조하는 방식 때문입니다.

변경 :

from MySQLdb import * 

당신이 그것을 당신이 패션을 참조하는 계획이라면

import MySQLdb 

합니다.

에서 : http://effbot.org/zone/import-confusion.htm

수입 X는 모듈 X를 가져, 현재 이름 공간에있는 해당 모듈에 대한 참조를 생성

어쨌든, 여기에 이러한 진술과 기능이 작동하는 방법입니다. 즉,이 문장을 실행 한 후에 X.name을 사용하여 모듈 X에 정의 된 것을 참조 할 수 있습니다.

from X import * 모듈 X를 가져오고 현재 네임 스페이스의 참조를 모두에게 만듭니다. 해당 모듈에 의해 정의 된 공용 객체 (즉, "_"로 시작하는 이름이없는 모든 것). 즉,이 문장을 실행 한 후에 모듈 X에 정의 된 것을 참조하기 위해 일반 이름을 사용하면됩니다.하지만 X 자체는 정의되어 있지 않으므로 X.name이 작동하지 않습니다. 이름이 이미 정의 된 경우 새 버전으로 바뀝니다. 그리고 X의 이름이 다른 객체를 가리 키도록 변경되면 모듈은 알아 채지 못합니다.

from X import a, b, c는 모듈 X를 가져오고 현재 네임 스페이스의 참조를 주어진 객체에 작성합니다. 즉, 이제는 프로그램에서 a와 b와 c를 사용할 수 있습니다.

+0

내 주, 미약 한 마음이 가져 오기를 잊어 버렸고 네임 스페이스에 영향을주는 방식을 잊어 버렸습니다. 내가 얼마나 바보인지 나에게 상기시켜 줘서 고마워. 당신의 설명은 평균 이상이었습니다! 나는이 교훈을 다시 잊지 않을 것입니다. – brad

1

from MySQLdb import *import MySQLdb do 매우 가지입니다.