Python安装MySQLdb

 

下载MySQL-python-1.2.3.tar.gz

 

进入到MySQL-python目录运行 python setup.py install 出现错误。

因为要使用setup.py需要安装python-setuptools

 

 

下面是setuptools安装:

· 下载 http://peak.telecommunity.com/dist/ez_setup.py

· 运行 python ez_setup.py

 

 

再一次进入到MySQL-python目录运行python setup.py install

再次出现错误:

 

sh: mysql_config: not found
Traceback (most recent call last):
  File "setup.py", line 15, in <module>
    metadata, options = get_config()
  File "/root/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/root/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

 

 

用find / -name mysql_config命令在系统里查找mysql_config,

结果为空,原因是libmysqlclient-dev库没有装上。

于是 apt-get install libmysqlclient-dev

再查询一次mysql_config文件,

可以得到该文件的地址,我这里是/usr/bin/mysql_config

编辑site.cfg文件,修改mysql_config变量为上面的地址:

 

mysql_config = /usr/bin/mysql_config
 

然后再运行python setup.py install,

又提示错误:

 

In file included from _mysql.c:29:
 pymemcompat.h:10:20: error: Python.h: No such file or directory
 _mysql.c:30:26: error: structmember.h: No such file or directory
 In file included from /usr/include/mysql/mysql.h:44,
                  from _mysql.c:40:
 .
 .
 .
 _mysql.c:2808: warning: return type defaults to 'int'
 _mysql.c: In function 'DL_EXPORT':
 _mysql.c:2808: error: expected declaration specifiers before 'init_mysql'
 _mysql.c:2886: error: expected '{' at end of input
 error: command 'gcc' failed with exit status 1

 

经过google,这里还需要装python-dev库,

果断安装之:

apt-get install python-dev

 

再一次python setup.py install,

终于成功装上MySQLDB:

 

nigelzeng@ubuntu:/usr/local/src/MySQL-python-1.2.3$ sudo python setup.py build     
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-i686-2.6/MySQLdb
running build_ext
building '_mysql' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-i686-2.6/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX
In file included from _mysql.c:36:
/usr/include/mysql/my_config.h:1088:1: warning: "HAVE_WCSCOLL" redefined
In file included from /usr/include/python2.6/Python.h:8,
                 from pymemcompat.h:10,
                 from _mysql.c:29:
/usr/include/python2.6/pyconfig.h:808:1: warning: this is the location of the previous definition
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-i686-2.6/_mysql.o -L/usr/lib/mysql -lmysqlclient_r -o build/lib.linux-i686-2.6/_mysql.so
 

 

最后install:

 

sudo python setup.py install

 

搞定!

 

+——————————————————————————————————————————+

setuptools:

 

介绍setuptools,就要从distutils讲起。Distutils属于Python的标准库,是从版本1.6以来就有的,其作用,主要是用来创建和使用安装包,我们常见的setup.py就是基于这个工具集创建,并且被这个工具集所使用的。这个模块的主要功能都在distutils.core里面,作为开发者,通常是导入distutils.core中的setup,来创建一个setup.py文件。

 

setuptools,则是在distutils基础上的一个增强包,提高了创建分发包,使用分发包的能力。在这个增强的模块里面,新增加了easy_install.py,同时开始支持egg文件的构建和使用。对开发者来说,增加了MANIFEST,PyReg,PYPI等特性。

 

这里,要提一下常用到的egg文件,egg文件,就像是java中的jar包,把代码和资源打包起来成为一个zip压缩包,并且冠于egg后缀。

 

你可能感兴趣的:(python,mysqldb)