ImportError: No module named MySQLdb 解决方案

在用PyCharm执行py脚本过程中出现ImportError: No module named MySQLdb 错误,经过查看发现是没有安装mysqldb。

 
环境:
        操作系统:WIN7 64位
        Python:Version 2.7
 
1、安装Python2.7(步骤略)
2、在环境变量的Path中配置Python2.7的安装路径

3、下载MySQL-Python-1.2.3.win-amd64-py2.7.exe进行安装,可以在http://download.csdn.NET/detail/seven_zhao/6607625下载

4、如果安装过程中出现python version 2.7 required,which was not found in the registry提示,并且下图中没有内容,则需要进行以下修复步骤

 

5、修复步骤4的错误提示:复制下面代码,保存为register.py,执行register.py文件(用python shell打开,按F5执行),执行完后就会出现python2.7 is already registered提示(忘了截图,借的图)

 

[python] view plain copy
 
 print?
  1.    
  2. import sys  
  3.    
  4. from _winreg import *  
  5.    
  6. # tweak as necessary  
  7. version = sys.version[:3]  
  8. installpath = sys.prefix  
  9.    
  10. regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)  
  11. installkey = "InstallPath"  
  12. pythonkey = "PythonPath"  
  13. pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (  
  14.     installpath, installpath, installpath  
  15. )  
  16.    
  17. def RegisterPy():  
  18.     try:  
  19.         reg = OpenKey(HKEY_CURRENT_USER, regpath)  
  20.     except EnvironmentError as e:  
  21.         try:  
  22.             reg = CreateKey(HKEY_CURRENT_USER, regpath)  
  23.             SetValue(reg, installkey, REG_SZ, installpath)  
  24.             SetValue(reg, pythonkey, REG_SZ, pythonpath)  
  25.             CloseKey(reg)  
  26.         except:  
  27.             print "*** Unable to register!"  
  28.             return  
  29.         print "--- Python", version, "is now registered!"  
  30.         return  
  31.     if (QueryValue(reg, installkey) == installpath and  
  32.         QueryValue(reg, pythonkey) == pythonpath):  
  33.         CloseKey(reg)  
  34.         print "=== Python", version, "is already registered!"  
  35.         return  
  36.     CloseKey(reg)  
  37.     print "*** Unable to register!"  
  38.     print "*** You probably have another Python installation!"  
  39.    
  40. if __name__ == "__main__":  
  41.     RegisterPy()  
 

 

 

6、继续执行步骤4,一直到完成。
7、此时再执行PyCharm中的脚本,如果出现”ImportError DLL load failed: %1 不是有效的 Win32 应用程序“错误提示则是因为安装的mysqldb是32位的MySql-python-1.2.3.win32-py2.exe,将其改成64位的再重新安装就可以了。

posted on 2017-05-19 16:39  DreamOnTheGo  阅读(658)  评论(0编辑  收藏  举报