Home  >  Article  >  Backend Development  >  Python下的Mysql模块MySQLdb安装详解

Python下的Mysql模块MySQLdb安装详解

WBOY
WBOYOriginal
2016-06-06 11:30:15938browse

默认情况下,MySQLdb包是没有安装的,不信? 看到类似下面的代码你就信了。

代码如下:

-bash-3.2# /usr/local/python2.7.3/bin/python get_cnblogs_news.py
Traceback (most recent call last):
  File "get_cnblogs_news.py", line 9, in
    import MySQLdb
ImportError: No module named MySQLdb


这时我们就不得不安装MySQLdb包了。安装其实也挺简单,具体步骤如下:
 1、下载 MySQL for Python
地址:http://sourceforge.net/projects/mysql-python/files/mysql-python/
我这里安装的是1.2.3版本

代码如下:

wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz


2、解压

代码如下:

tar zxvf MySQL-python-1.2.3.tar.gz


3、安装

代码如下:

$ cd MySQL-python-1.2.3
$ python setup.py build
$ python setup.py install


注:
如果在执行:python setup.py build 遇到以下错误:

代码如下:

EnvironmentError: mysql_config not found


首先查找mysql_config的位置,使用
find / -name mysql_config ,比如我的在/usr/local/mysql/bin/mysql_config
修改setup_posix.py文件,在26行:
mysql_config.path = “mysql_config” 修改为:

代码如下:

mysql_config.path = “/usr/local/mysql/bin/mysql_config”


保存后,然后再次执行:

代码如下:

python setup.py build
python setup.py install


OK,到此大功告成。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn