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提示(忘了截图,借的图)
- 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()
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 阅读(700) 评论(0) 编辑 收藏 举报