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後綴。

 

發佈了55 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章