在Win7中使用Python的MySQLdb模块

概述:

  Linux上对这一块的处理还是不错的,不过在Windows上就有一点小麻烦,麻烦的点不在于安装过程,而是在安装的过程中可能会有一些问题。


步骤:

1.安装MySQLdb模块

  我们在网上下载相应的MySQLdb的版本文件,例如我的就是MySQL-python-1.2.3.win-amd64-py2.7。此文件是exe文件,直接点击运行即可。


2.解决python version 2.7 required,which was not found in the registry报错


  在我想下载完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/distutils-sig@python.org/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,搞定。


3.解决DLL load failed: %1 不是有效的 Win32 应用程序报错


  出现上述问题的原因是因为我们的Python和MySQLdb的版本不对应造成的。我的问题是Python是32位的,而MySQLdb却是64位的。

原文地址:https://www.cnblogs.com/fengju/p/6336071.html