python安装MySQLdb模块遇到的坑及解决办法

安装python的MySQL依赖包


在使用python操做MySQL数据库的时候,须要安装MySQL模块进行操做,即MySQLdb。可是在安装MySQL模块的时候总会遇到问题:python

  • 一、使用MySQL-python-1.2.5.tar包进行安装,提示须要setuptools工具
  • 二、执行python setup.py install的时候,提示安装环境不对
  • 三、经过pip install mysql,提示和第二条同样

安装过程

这是我安装MySQL-python-1.2.5.tar报的错误:mysql

_mysql.c:29:20: error: Python.h: No such file or directory
_mysql.c:40:26: error: structmember.h: No such file or directory
...

通过测试,发现是由于mysql-devel与python-devel没有安装。
mysql-devel安装没有问题,而且有MySQL服务的机器通常都会有mysql-devel环境
安装python-devel的过程遇到问题(这个问题我在新建立的虚拟机上测试是没有的),在生产环境中遇到问题,提示glibc依赖关系错误。
而后我尝试下载python-devel的rpm包安装,提示:sql

error: Failed dependencies:
python = 2.6.6-66.e16_8 is needed by python-devel-2.6.6-66.e16_8.x86_64
ptrhon-libs(x86_64) = 2.6.6-66.e16_8 is needed by python-devel-2.6-66.e16_8.x86_64
python < 2.6.6-66.e16_8 conflicts with python-devel-2.6.6-66.e16_8.x86_64

到了这里,我没法找到相应的问题,考虑从其余方式来安装,最后成功:shell

安装步骤:
1  安装pip
yum -y install python-pip.noarch
确认已有安装包:
pip list

2  安装python 访问mysql工具
yum -y install MySQL-python*

3 确认python访问mysql组件
pip list | grep "MySQL"
python 
>> import MySQLdb

成功安装!

做者 [congshenV][1]
2017 年 09月 11日数据库

相关文章
相关标签/搜索