python安装MySQLdb / mysql-python模块遇到的错误题目及解决

打印 上一主题 下一主题

主题 521|帖子 521|积分 1563

一、错误 ImportError: No module named MySQLdb

     python下安装mysql模块mysql-python时碰到 报错:ImportError: No module named MySQLdb。在新环境下部署python脚本时肯定会常常遇到缺少模块的报错,比如:
   ImportError: No module named daemon
ImportError: No module named beanstalkc
ImportError: No module named MySQLdb
ImportError: No module named pymongo
ImportError: No module named pyssdb
      这时我们就须要安装第三方模块,安装第三方模块有多种办法,比如可以直接下载第三方库包,解压后找到setup.py文件,然后运行python setup.py install即可完成安装,这个方法须要手动下载第三方模块包文件,有些麻烦,另一个非常好用的方法就是利用pip。pip是python库安装的整套服务的解决办法,有点和linux中的yum雷同,利用pip前先安装pip。
  1. $ wget https://pypi.python.org/packages/source/p/pip/pip-1.2.1.tar.gz
  2. $ tar xzf pip-1.2.1.tar.gz
  3. $ cd pip-1.2.1.tar.gz
  4. $ python setup.py install
复制代码
     先利用python  setup.py安装pip。固然也可以利用yum安装,之后全部的python模块都可以利用pip安装了。非常方便,本日碰到的这个题目是MySQLdb扩展,执行pip install MySQLdb
  1. [webserver@localhost pip-1.2.1]$ sudo pip install MySQLdb
  2. Downloading/unpacking MySQLdb
  3.   Could not find any downloads that satisfy the requirement MySQLdb
  4. No distributions at all found for MySQLdb
  5. Storing complete log in /root/.pip/pip.log
复制代码
     会看到报错提示,没有找到MySQLdb的安装包的信息,后来才相识到,要安装mysql须要利用pip install mysql-python。以是这个包名真麻烦啊,提示的缺少安装包和真实安装的包名不一致,很欠好弄。不外pip也有方法的,可以利用pip search 包名,来查找相关的包后再安装。另外在安装mysql-python时遇到报错:
  1. gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPI
  2. C -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dv
  3. ersion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-x86_64-2.6/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOUR
  4. CE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUN
  5. IV_LINUX
  6.     _mysql.c:29:20: error: Python.h: No such file or directory
  7.     _mysql.c:40:26: error: structmember.h: No such file or directory
  8.     _mysql.c:74: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  9.     _mysql.c:75: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  10.     _mysql.c:76: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  11.     _mysql.c:77: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  12.     _mysql.c:78: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  13.     _mysql.c:79: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  14.     _mysql.c:80: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  15.     _mysql.c:81: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  16.     _mysql.c:82: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  17.     _mysql.c:83: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  18.     _mysql.c:84: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
  19.     _mysql.c:87: error: expected specifier-qualifier-list before ‘PyObject_HEAD’
  20.     _mysql.c:97: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_mysql_ConnectionObject_Type’
  21.     _mysql.c:100: error: expected specifier-qualifier-list before ‘PyObject_HEAD’
复制代码
     上面提示找不到python.h,这是由于在利用Python举行开发时须要安装一些依赖包,而在利用yum安装的python时并没有安装这些包,以是在执行安装mysql-python前须要先安装:
  1. yum install python-devel
复制代码
     须要留意的是在CentOS下叫做 python-devel,而在ununbtu中包名叫python-dev。另外在安装beanstalkc模块的时候总是安装失败,在网上找了找,安装beanstalkc前还须要安装一个包,在此做个记录,如下:
  1. pip install pyyaml
  2. pip install beanstalkc
复制代码
二、pip install mysql-python时报错sh: mysql_config: command not found

pip install mysql-python时报错sh: mysql_config: command not found:
  1. [root@kermit python]# pip install mysql-python
  2. Collecting mysql-python
  3.   Using cached MySQL-python-1.2.5.zip
  4.     Complete output from command python setup.py egg_info:
  5.     sh: mysql_config: command not found
  6.     Traceback (most recent call last):
  7.       File "<string>", line 1, in <module>
  8.       File "/tmp/pip-build-WiuGVK/mysql-python/setup.py", line 17, in <module>
  9.         metadata, options = get_config()
  10.       File "setup_posix.py", line 43, in get_config
  11.         libs = mysql_config("libs_r")
  12.       File "setup_posix.py", line 25, in mysql_config
  13.         raise EnvironmentError("%s not found" % (mysql_config.path,))
  14.     EnvironmentError: mysql_config not found
复制代码
  解决办法:yum install python-devel
      #python和python-dev以及boost.python的关系。python是执行环境如果你要c/c++中调用python,或者python调用c或者c++,则须要python-dev。boost.python仅仅是对python-dev举行了封装,很多函数,双方都具有同样的功能,boost.python的接口封装得更为友爱而已如异常处理。
yum install mysql-devel
    #MySQL-devel是mysql所需的库和包含文件。如果想要编译其他MySQL客户步伐, 例如Perl模块,python模块就须要安装这个。MySQL for Python library在Python中也就相当于JAVA中对应MySQL的JDBC Driver

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

伤心客

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表