0
即使在安裝連接器之後,我在pyhton 2.7中執行下面的代碼時也會遇到導入錯誤。請幫助我收到錯誤消息。 ImportError:沒有名爲MySQLdb的模塊
import MySQLdb
導入錯誤:沒有名爲MySQLdb的
即使在安裝連接器之後,我在pyhton 2.7中執行下面的代碼時也會遇到導入錯誤。請幫助我收到錯誤消息。 ImportError:沒有名爲MySQLdb的模塊
import MySQLdb
導入錯誤:沒有名爲MySQLdb的
模塊我也許能幫助你。我不知道你使用的是什麼操作系統,但目前我在Ubuntu,並且沒有本地安裝MySQLdb的模塊,所以我安裝了它:
sudo apt-get install python-MySQLdb
安裝後,我編寫這個小函數來檢查MySQLdb是否已成功導入到文件中。我這樣做使用sys.modules
。
import MySQLdb
import sys
def test_module_import():
while True:
print "Would you like to test if the module has been imported?"
retest = raw_input("Press [Y] to continue or [N] to exit ").lower()
if retest == 'y':
if 'MySQLdb' in sys.modules:
print "Module imported!"
continue
else:
print "Module NOT imported!"
continue
elif retest == 'n':
sys.exit()
break
else:
print "Not a valid command."
continue
if __name__ == '__main__':
test_module_import()
`
這證實了我的模塊是,實際上被導入。自己嘗試一下。我相信你知道這一點,但確保MySQLdb已正確安裝在Python的可用模塊中。