一、错误 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。
- $ wget https://pypi.python.org/packages/source/p/pip/pip-1.2.1.tar.gz
- $ tar xzf pip-1.2.1.tar.gz
- $ cd pip-1.2.1.tar.gz
- $ python setup.py install
复制代码 先利用python setup.py安装pip。固然也可以利用yum安装,之后全部的python模块都可以利用pip安装了。非常方便,本日碰到的这个题目是MySQLdb扩展,执行pip install MySQLdb
- [webserver@localhost pip-1.2.1]$ sudo pip install MySQLdb
- Downloading/unpacking MySQLdb
- Could not find any downloads that satisfy the requirement MySQLdb
- No distributions at all found for MySQLdb
- Storing complete log in /root/.pip/pip.log
复制代码 会看到报错提示,没有找到MySQLdb的安装包的信息,后来才相识到,要安装mysql须要利用pip install mysql-python。以是这个包名真麻烦啊,提示的缺少安装包和真实安装的包名不一致,很欠好弄。不外pip也有方法的,可以利用pip search 包名,来查找相关的包后再安装。另外在安装mysql-python时遇到报错:
- 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
- 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
- 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
- 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
- IV_LINUX
- _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.c:74: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:75: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:76: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:77: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:78: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:79: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:80: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:81: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:82: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:83: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:84: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
- _mysql.c:87: error: expected specifier-qualifier-list before ‘PyObject_HEAD’
- _mysql.c:97: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_mysql_ConnectionObject_Type’
- _mysql.c:100: error: expected specifier-qualifier-list before ‘PyObject_HEAD’
复制代码 上面提示找不到python.h,这是由于在利用Python举行开发时须要安装一些依赖包,而在利用yum安装的python时并没有安装这些包,以是在执行安装mysql-python前须要先安装:
须要留意的是在CentOS下叫做 python-devel,而在ununbtu中包名叫python-dev。另外在安装beanstalkc模块的时候总是安装失败,在网上找了找,安装beanstalkc前还须要安装一个包,在此做个记录,如下:
- pip install pyyaml
- pip install beanstalkc
复制代码 二、pip install mysql-python时报错sh: mysql_config: command not found
pip install mysql-python时报错sh: mysql_config: command not found:
- [root@kermit python]# pip install mysql-python
- Collecting mysql-python
- Using cached MySQL-python-1.2.5.zip
- Complete output from command python setup.py egg_info:
- sh: mysql_config: command not found
- Traceback (most recent call last):
- File "<string>", line 1, in <module>
- File "/tmp/pip-build-WiuGVK/mysql-python/setup.py", line 17, in <module>
- metadata, options = get_config()
- File "setup_posix.py", line 43, in get_config
- libs = mysql_config("libs_r")
- File "setup_posix.py", line 25, in mysql_config
- raise EnvironmentError("%s not found" % (mysql_config.path,))
- 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企服之家,中国第一个企服评测及商务社交产业平台。 |