Linux上對這一塊的處理還是不錯的,不過在Windows上就有一點小麻煩,麻煩的點不在於安裝過程,而是在安裝的過程中可能會有一些問題。
我們在網上下載相應的MySQLdb的版本文件,例如我的就是MySQL-python-1.2.3.win-amd64-py2.7。此文件是exe文件,直接點擊運行即可。
在我想下載完MySQL-python-1.2.3.win-amd64-py2.7.exe進行安裝時,程序給我報了這樣一個錯誤信息:
win7是64位的原因,在安裝python時,如果選擇只為當前用戶,以上問題是不會出現的,如果選擇所有用戶,那就用上面的方法解決吧
1.復制下面的代碼,保存至register.py:
# # script to register Python 2.0 or later for use with win32all # and other extensions that require Python registry settings # # written by Joakim Loew for Secret Labs AB / PythonWare # # source: # http://www.pythonware.com/products/works/articles/regpy20.htm # # modified by Valentine Gogichashvili as described in http://www.mail-archive.com/[email protected]/msg10512.html import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version) installkey = "InstallPath" pythonkey = "PythonPath" pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath ) def RegisterPy(): try: reg = OpenKey(HKEY_CURRENT_USER, regpath) except EnvironmentError as e: try: reg = CreateKey(HKEY_CURRENT_USER, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print "*** Unable to register!" return print "--- Python", version, "is now registered!" return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print "=== Python", version, "is already registered!" return CloseKey(reg) print "*** Unable to register!" print "*** You probably have another Python installation!" if __name__ == "__main__": RegisterPy()
2.運行register.py,搞定。
出現上述問題的原因是因為我們的Python和MySQLdb的版本不對應造成的。我的問題是Python是32位的,而MySQLdb卻是64位的。