天天看點

Python的MySQLdb子產品安裝

系統:ubuntu 11.10

mysql:直接apt-get安裝的,version:5.1.62

然後解壓,打開README:

裡面有安裝過程:

  tarxfzMySQL−python−1.2.1.tar.gztarxfzMySQL−python−1.2.1.tar.gz cd MySQL-python-1.2.1 

  # edit site.cfg if necessary# edit site.cfg if necessary python setup.py build 

  $ sudo python setup.py install # or su first

不過在python setup.py build時報錯:

ImportError: No module named setuptools

ubuntu下安裝:

sudo apt-get install python-setuptools

python-setuptools : Python Distutils Enhancements (setuptools compatibility)

然後再次python setup.py build,又報錯:

EnvironmentError: mysql_config not found

因為mysql_config是屬于MySQL開發用的檔案,而使用apt-get安裝的MySQL是沒有這個檔案的,于是在包安裝器裡面尋找

sudo apt-get install  libmysqld-dev

libmysqld-dev : MySQL embedded database development files

再次運作python setup.py build,報錯:

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.7 -c _mysql.c -o build/temp.linux-i686-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX 

In file included from _mysql.c:29:0: 

pymemcompat.h:10:20: fatal error: Python.h: No such file or directory

解決方案,

sudo apt-get install python-dev

python-dev : header files and a static library for Python (default)

然後就按照README裡的:

  pythonsetup.pybuildpythonsetup.pybuild sudo python setup.py install

測試:

>>>import MySQLdb

沒有報錯即可。

參考:

<a href="http://blog.csdn.net/guzicheng/article/details/5884106" target="_blank">http://blog.csdn.net/guzicheng/article/details/5884106</a>

<a href="http://be-evil.org/post-185.html" target="_blank">http://be-evil.org/post-185.html</a>

<a href="http://hi.baidu.com/hevensun/blog/item/3b6e313fe8515df154e7238f.html" target="_blank">http://hi.baidu.com/hevensun/blog/item/3b6e313fe8515df154e7238f.html</a>

<a href="http://stackoverflow.com/questions/1449130/how-to-install-mysqldb-package-importerror-no-module-named-setuptools" target="_blank">http://stackoverflow.com/questions/1449130/how-to-install-mysqldb-package-importerror-no-module-named-setuptools</a>

<a href="http://www.cnblogs.com/objdump/archive/2011/11/03/mysql_python_install_error.html" target="_blank">http://www.cnblogs.com/objdump/archive/2011/11/03/mysql_python_install_error.html</a>

==============================================================================

本文轉自被遺忘的部落格園部落格,原文連結:http://www.cnblogs.com/rollenholt/archive/2012/05/07/2487137.html,如需轉載請自行聯系原作者