安装 MySQL-python 出错(转)

开始出错:

安装 MySQL-python 出错

# python2.6 setup.py install
Traceback (most recent call last):
File "setup.py", line 5, in <module>
from setuptools import setup, Extension
ImportError: No module named setuptools

处理方法,安装setuptools

 
  
  1. wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz 
  2.   
  3. tar zxvf setuptools-0.6c11.tar.gz 
  4. cd setuptools-0.6c11 
  5. python2.6 setup.py install 

 error: command 'gcc' failed with exit status 1

接着出错:

为了安装mysql for python,从sourceforge下载了三遍安装文件,每次进入目录中执行 python setup.py install,总是报错

如下:

serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_ke
y'])
WindowsError: [Error 2]

 

,google了下,一般解释是regedit中没有设置,在SO中也有人提出这样的问题(link),看来是个不小的bug。

 

各种修改无果。继续google之,看到还是有解决方案的呀(link),果断从codegood下载到for python 2.7的安装文件,安装后,测试OK

 

简单连接mysql,测试通过

 1 #-*- encoding:gb2312  -*-
2 import os,sys,string
3 import MySQLdb
4
5 try:
6 conn = MySQLdb.connect(host = "localhost",user="root",passwd = "123",db="information_schema")
7 except Exception,e:
8 print e
9 sys.exit()
10
11 cursor = conn.cursor()
12
13 #sql = "create table if not exists test1(name varchar(120)primary key,age int(4))"
14 #cursor.execute(sql)
15 #中文
16 #sql = "if exists test 1 drop table test1"
17 #cursor.execute(sql)
18
19 #sql = "truncate table firsttable"
20 #cursor.execute(sql)
21
22 #for i in [1,2,4,5,6]:
23 # sql = """insert into firsttable (name) values('liu')"""
24 # cursor.execute(sql)
25
26 cursor.execute("select * from character_sets");
27 resultset = cursor.fetchall();
28 print resultset # pass, python is good at linking to mysql
29
30 cursor.close()
31 conn.close();
32

测试通过,下面用django把框架搭起来,菜鸟准备继续前进。

链接:

今天一晚上的时间都浪费在django使用mysql上了!问题一个接着一个,解决方案A一一失败,现在正在实施方案B,解决完就记录下来,然后睡觉去!

 

    我的问题是,我在windows xp上使用python 2.7 + django 1.2.5 + mysql 5.5出现的问题。

 

1. 首先是在python manage.py syncdb 时出现了错误:

......

    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo
dule named MySQLdb


2. 原来是MySQLdb没有安装,google了一把,要安装这个东西,要从这里下载:

http://sourceforge.net/projects/mysql-python/

下载最新的版本,然后按老样子,进行安装,又出现下面的问题:


3. python setup.py build install

Traceback (most recent call last):
  File "setup.py", line 15, in <module>
    metadata, options = get_config()
  File "D:\Jeffrey\Development\Software\MySQL-python-1.2.3\setup_windows.py", li
ne 7, in get_config
    serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_ke
y'])
WindowsError: [Error 2]

这一步又是google了半天,历经千辛万苦,找到下面的解决方案:

首先在setup_windows.py里看到:

    serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])
    mysql_root, dummy = _winreg.QueryValueEx(serverKey,'Location')

就是在注册表找不到registry_key,这是在site.cfg里定义的:

registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0,按这篇文章修改了一下site.cfg成:

registry_key = SOFTWARE\MySQL AB\MySQL Server 5.5

接下去运行,又出错了!我晕啊,难道是上天在考验我的毅力吗?


4. unable to find vcvarsall.bat

running build
running build_py
copying MySQLdb\release.py -> build\lib.win32-2.7\MySQLdb
running build_ext
building '_mysql' extension
error: Unable to find vcvarsall.bat

解决方案可以参考这里

还有这里

简单来说,就是要这样:

解决“Unable to find vcvarsall.bat”错误
   1. 首先安装MinGW,在MinGW的安装目录下找到bin的文件夹,找到mingw32-make.exe,复制一份更名为make.exe;
   2. 把MinGW的路径添加到环境变量path中,比如我把MinGW安装到D:\MinGW\中,就把D:\MinGW\bin添加到path中;
   3. 打开命令行窗口,在命令行窗口中进入到要安装代码的目录下;
   4. 输入如下命令就可以安装了。
setup.py install build --compiler=mingw32

上帝啊,这次应该over了吧!别再折磨人了!结果:

building '_mysql' extension
D:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -Dversion_info=(1,2,3,'final',0)
 -D__version__=1.2.3 "-ID:\Program Files\MySQL\MySQL Server 5.5\include" -ID:\Py
thon27\include -ID:\Python27\PC -c _mysql.c -o build\temp.win32-2.7\Release\_mys
ql.o
_mysql.c:34:24: fatal error: config-win.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

 

    这下我彻底崩溃了!有没有天理啊!

 

    以上统称为方案A,失败!真是失败!

 

    然后看到有篇文章写道:

Installing mysql module for python 
I rate this mission as : impossible for the everyday developer on windows. 
Here are several of the problems I got along the way :

_mysql.c:34:24: fatal error: config-win.h: No such file or directory
serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])
WindowsError: [Error 2] The system cannot find the file specified
error: Unable to find vcvarsall.bat

Save yourself the headache -
Search for a "download compiled python 2.7 mysql for windows"
change the query to your python version. 
Do not stop until you find a file named 
"MySQL-python-1.2.3.win32-py2.7.exe"

again - version might be different. 

Do not go on the mission of installing it by yourself, it is simply a nightmare.

 

    哇咔咔,早说啊!现在就这方案了!在google上找:"download compiled python 2.7 mysql for windows"

    果然,找到了:

http://www.codegood.com/archives/129

    直接下载那个MySQL-python-1.2.3.win32-py2.7.exe文件安装吧,简单的解决花了我一晚上的时间的问题吧!

 

    回过头来,想想今天一晚上真是冤枉!没头没脑的被浪费了。下次解决类似问题,要先停下来想一想,不要一根筋的死去搞了。python\mysql这一块确实有点让初学者头痛,再想想之前在公司搞的python/oracle也是一番周折。我觉得python应该学学ruby,在易用性上更多的考虑考虑用户的体验。好了,现在问题解决了,睡吧。

所有问题都解决了 只要下载个 MySQL-python-1.2.3.win32-py2.7.exe就可以了 不用其他的 直接安装就能链接mysql了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值