python27安装mysqldb

1:安装vcforpython27.msi
2:在aconda的python27环境下执行python reg.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()

3:安装MySQL-python-1.2.4b4.win32-py2.7.exe
4:在aconda的python27环境下执行pip install mysql
如果以上执行完后,执行pyhton
import MySQLdb
应该不报错了
测试:

#!/use/bin/python
#coding=utf-8

import MySQLdb

# 打开数据库连接  url,username,password,database
db = MySQLdb.connect("localhost","root","root","cacti" )

# 使用cursor()方法获取操作游标
cursor = db.cursor()

# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()

print "Database version : %s " % data

# 关闭数据库连接
db.close()

附:

首先,在python2.7中用到import mysqldb的时候,提示你缺少这个模块,那么你需要pip install mysql-python,但使用windows系统在pip这个模块的时候可能会出现一些问题,如下:

用pip install mysql-python出现问题

(1)出现缺少Microsoft VC++ 9.0的问题,是缺少对于python2.7的c语言的编译解释器

用VCForPython27这个工具可以解决这个问题

(2)出现Cannot open include file: ‘config-win.h’: No such file or directory这个问题,是缺少MySQL-python-1.2.5-py2.7这个第三方类库,安装之后可以解决

(3)在使用python的第三方类库的时候,会出现python2.7不在注册表内,报错:python version 2.7 required,which was not found。是因为两种原因,一种是没有配置python的环境变量,他真的不在注册表里面,另一种这是因为,python和第三方类库不都是32位或64位。所以要统一格式

比如:

MySQL-python-1.2.3.win32-py2.7

MySQL-python-1.2.5.win-amd64-py2.7

上面的是32位,下面的是64位

(1)(2)(3)方案解决方案中的工具在我的云盘分享的连接里可以下载到

链接:https://pan.baidu.com/s/1_GvIWoJByOyIyTYvDfADPQ 密码:q1ql

参考链接:
https://blog.csdn.net/qq_34288630/article/details/80267263
https://blog.csdn.net/NepalTrip/article/details/79272646
https://blog.csdn.net/zzy296753977/article/details/80848632

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值