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位的再重新安装就可以了。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值