且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python 2.7中安装mysql

更新时间:2022-09-21 17:51:45

在python中进行安装mysql模块,但是怎么都不能导入mysql模块,出错如下所示:


[python] view plain copy

  1. [root@python ~]# python  

  2. Python 2.7.11 (default, Apr  5 201612:24:31)   

  3. [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2  

  4. Type "help""copyright""credits" or "license" for more information.  

  5. >>> import MySQLdb  

  6. Traceback (most recent call last):  

  7.   File "<stdin>", line 1in <module>  

  8. ImportError: No module named MySQLdb  


检查安装包:



[python] view plain copy

  1. [root@python ~]# rpm -qa MySQL-python  

  2. MySQL-python-1.2.3-0.3.c1.1.el6.x86_64  


发现在操作系统中已经安装了Mysql-python的包,但是依然不能导入的。。。



安装mysql-python模块:


[python] view plain copy

  1.  error: command 'gcc' failed with exit status 1  

  2.       

  3.     ----------------------------------------  

  4. Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-OcAYDJ/mysql-python/setup.py';  

  5. exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n''\n'), __file__, 'exec'))"   

  6. install --record /tmp/pip-Urb3hW-record/install-record.txt --single-version-externally-managed --compile"   

  7. failed with error code 1 in /tmp/pip-build-OcAYDJ/mysql-python/  


在进行pip jinstall musql-python的时候就是出错!!!报错内容如上。



出错的原因是因为必须安装mysql-python包,从而安装此包:


[python] view plain copy

  1. [root@python ~]# yum -y install mysql-devel  


再次进行安装mysql-python模块:


[python] view plain copy

  1. [root@python ~]# pip install mysql-python  

  2. Collecting mysql-python  

  3.   Using cached MySQL-python-1.2.5.zip  

  4. Installing collected packages: mysql-python  

  5.   Running setup.py install for mysql-python ... done  

  6. Successfully installed mysql-python-1.2.5  


成功安装。



在使用pip安装的模块的时候,如果需要卸载,那么只要pip uninstall packagename即可,还是蛮方便的。


此问题在2.7版本中发现,在2.6版本中仅仅需要安装mysql-python包即可导入。


[python] view plain copy

  1. [root@python ~]# python  

  2. Python 2.7.11 (default, Apr  5 201612:24:31)   

  3. [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2  

  4. Type "help""copyright""credits" or "license" for more information.  

  5. >>> import MySQLdb  

  6. >>>   

本文转自不要超过24个字符博客51CTO博客,原文链接http://blog.51cto.com/cstsncv/1968821如需转载请自行联系原作者

cstsncv