By default, the MYSQLDB package is not installed, do not believe? You'll believe it if you see something like the following code.
-bash-3.2#/usr/local/python2.7.3/bin/python get_cnblogs_news.py
Traceback (most recent):
File "get_cnblogs_news.py", line 9, <module>
Import MySQLdb
Importerror:no module named MySQLdb
Then we had to install the MYSQLDB package. Installation is actually quite simple, the specific steps are as follows:
1. Download MySQL for Python
Address: http://sourceforge.net/projects/mysql-python/files/mysql-python/
I installed here is 1.2.3 version
wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
2. Decompression
Tar zxvf mysql-python-1.2.3.tar.gz
3. Installation
$ CD mysql-python-1.2.3
$ python setup.py Build
But in the Python setup.py build times wrong:
Importerror:no module named Setuptools
Install under Ubuntu:
sudo apt-get install Python-setuptools
Python-setuptools:python distutils Enhancements (setuptools compatibility)
Then again python setup.py build, and Error:
Environmenterror:mysql_config not found
Because Mysql_config is a MySQL development file, and MySQL with Apt-get installed does not have this file, so in the package installer to find
sudo apt-get install Libmysqld-dev
Libmysqld-dev:mysql Embedded database development files
Run Python setup.py build again, error:
Building ' _mysql ' extension
Gcc-pthread-fno-strict-aliasing-dndebug-g-fwrapv-o2-wall-wstrict-prototypes-fpic-dversion_info= (the "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
Solutions,
sudo apt-get install Python-dev
Python-dev:header files and a static library for Python (default)
And then just follow the Readme:
Pythonsetup.pybuild
sudo python setup.py install
Test:
>>>import MySQLdb
No error is required.
Reference:
http://blog.csdn.net/guzicheng/article/details/5884106
Http://be-evil.org/post-185.html
Http://hi.baidu.com/hevensun/blog/item/3b6e313fe8515df154e7238f.html
Http://stackoverflow.com/questions/1449130/how-to-install-mysqldb-package-importerror-no-module-named-setuptools
Http://www.cnblogs.com/objdump/archive/2011/11/03/mysql_python_install_error.html
MYSQLDB Installation Guide