python安装MySQLdb 遇到的问题汇总

因为对mysql相对熟悉一些,就准备用mysql来作为项目数据库,本以为安装也是手到擒来,没想到一安装都是各种报错,折腾一晚上才安装成功。做笔记以备忘。

情况一: 运行 pip install mysql-python 提示 Microsoft Visual C++ 9.0 is required

解决方案:前往:https://www.microsoft.com/en-us/download/confirmation.aspx?id=44266 下载 VCForPython27.msi 程序,傻瓜式安装

 

情况二:下载 MySQL-python-1.2.5.win-amd64-py2.7.exe 安装mysql ,结果第一步就提示python 2.7 没有注册,但是我的确已经下载了,百度万能:

# 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()

保存文件到桌面并命名为 Register.py, python 运行该文件,会提示已经注册

情景三,以为总算安装成功了mysql了嘛,没有,在 views.py 试着 import mysql模块:

from django.shortcuts import render_to_response
import datetime
import MySQLdb

def temp_test(request):
	now = datetime.datetime.now()
	return render_to_response('temp_test.html', locals())

python安装MySQLdb 遇到的问题汇总_第1张图片

又提示DLL 不是有效的Win32应用程序

解决方法:下载对应64位的pywin32

最后运行正常

 

/*********************************************************************************/

扩展:装了mysql后,再次启动phpstudy的时候,发现两者数据库之间发生了冲突,phpstudy的mysql无法启动,通过查找计算机运行的mysql的程序:

win+R---->regedit---->HKEY_LOCAL_MACHINE---->SYSTEM---->CurrentControlSet---->Services---->MySQL

把imagePath 对应的值改成phpstudy对应的mysql 下的 bin 目录下,重启,正常。

你可能感兴趣的:(python学习笔记)