python脚本执行时报ImportError: No module named MySQLdb的处理

时间:2021-05-04 23:16:17

一.问题描述

执行py脚本时,提示ImportError: No module named MySQLdb错误,如下:
[root@mvxl0783 sh]# python 1.py
Traceback (most recent call last):
File "1.py", line 4, in <module>
import MySQLdb
ImportError: No module named MySQLdb

版本:centOS 6.6

二.问题分析
先查看当前python版本
[root@mvxl0783 sh]# python -V
Python 2.7.13
[root@mvxl0783 sh]# python
Python 2.7.13 (default, Apr 11 2017, 17:58:38) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

查看OS安装的python相关情况:
[root@mvxl0783 sh]# rpm -qa|grep python
.....
.....
MySQL-python-1.2.3-0.3.c1.1.el6.x86_64
python-2.6.6-52.el6.x86_64
......
就是说OS存在两个python版本2.6.6和2.7.13,猜测与多个版本同时存在会引起上面报错,所以尝试删除2.6.6版,先是删除了MySQL-python,再删除python,报错如下:
yum remove python-2.6.6-52.el6.x86_64
........................
.........................
--> Running transaction check
---> Package redhat-lsb-compat.x86_64 0:4.0-7.el6.centos will be erased
--> Finished Dependency Resolution
Error: Trying to remove "yum", which is protected
提示yum有保护,不能移除,采用跳过相依赖包来直接删除:rpm -e --nodeps python-2.6.6-52.el6.x86_64 成功,再安装MySQL-python,却提示如下:
[root@mvxl0783 sh]# yum install MySQL-python
-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

查看yum和python的rpm是否安装,确认系统已安装yum和python的的rpm包。
查看一下系统命令:
#yum命令存在
[root@mvxl0783 sh]# ll /usr/bin/yum 
-rwxr-xr-x. 1 root root 801 Oct 16 2014 /usr/bin/yum

[root@mvxl0783 bin]# pwd
/usr/bin
[root@mvxl0783 bin]# ls -al|grep python
-rwxr-xr-x. 1 root root 11232 Oct 16 2014 abrt-action-analyze-python

原来python的软连接源文件已经不存在,所以出现上面错误:
重新做一个python的软连接:
# ln -s /usr/local/python27/bin/python /usr/bin/python
由于yum只兼容python 2.6,而不兼容2.7,所以老版本还是要找回来,等于现在又要走回去,重头再来。
cd /usr/local/src/
mkdir Python-2.6.6
cd /media/Packages (在os安装盘中)
cp python-2.6.6-52.el6.x86_64.rpm /usr/local/src/Python-2.6.6/
cp python-urlgrabber-3.9.1-9.el6.noarch.rpm /usr/local/src/Python-2.6.6/
cp python-devel-2.6.6-52.el6.x86_64.rpm /usr/local/src/Python-2.6.6/
cp python-libs-2.6.6-52.el6.x86_64.rpm /usr/local/src/Python-2.6.6/
cp yum-3.2.29-60.el6.centos.noarch.rpm /usr/local/src/Python-2.6.6/
cd /usr/local/src/Python-2.6.6/
[root@mvxl0783 Python-2.6.6]# rpm -Uvh --replacepkgs *.rpm
warning: python-2.6.6-52.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
1:python-libs ########################################### [ 20%]
2:python ########################################### [ 40%]
3:python-urlgrabber ########################################### [ 60%]
4:yum ########################################### [ 80%]
5:python-devel ########################################### [100%]

更改yum的python版本:
vi /usr/bin/yum
#!/usr/bin/python改为#!/usr/bin/python-2.6.6
将python改名:
[root@mvxl0783 bin]# mv /usr/bin/python /usr/bin/python-2.6.6
[root@mvxl0783 bin]# ln -s /usr/local/python27/bin/python /usr/bin/python

再安装:yum install MySQL-python 安装成功!
[root@mvxl0783 sh]# rpm -qa|grep MySQL-python 
MySQL-python-1.2.3-0.3.c1.1.el6.x86_64

执行py脚本时,依然提示:ImportError: No module named MySQLdb错误,说明这个与有多个python版本同时存在没有关系。

三.问题解决
怀疑是python版本与MySQL-python版本不一致引起,所以从网上下载python 2.7.13对应的MySQL-python-1.2.5进行安装,如下:
[root@mvxl0783 MySQL-python-1.2.5]# python setup.py build
Traceback (most recent call last):
File "setup.py", line 7, in <module>
import setuptools
ImportError: No module named setuptools
这次提示ImportError: No module named setuptools,说明没有找到setuptools包,需要下载安装:
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar zxvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build ----正常完成
python setup.py install ----正常完成
同时需要安装:yum install python-devel。
root@mvxl0783 MySQL-python-1.2.5]# python setup.py build
sh: mysql_config: command not found
Traceback (most recent call last):
File "setup.py", line 17, in <module>
metadata, options = get_config()
File "/data/tool/MySQL-python-1.2.5/setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "/data/tool/MySQL-python-1.2.5/setup_posix.py", line 25, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not foun
这次抛出的错误与上面不同,从提示看,说明有个文件属性没打开,修改site.cfg,将mysql_config这个属性打开,如下:
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)

embedded = False
threadsafe = True
static = False

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /apps/svr/mariadb10/bin/mysql_config

注意以上mysql_config那行,与安装的mysql目录有关,自己去找自己安装的mysql目录即可。

[root@mvxl0783 MySQL-python-1.2.5]# python setup.py build

running build
running build_py
creating build
......
成功完成.
[root@mvxl0783 MySQL-python-1.2.5]#python setup.py install
running install
running bdist_egg
running egg_info
..................
..................
Installed /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg
Processing dependencies for MySQL-python==1.2.5
Finished processing dependencies for MySQL-python==1.2.5
成功完成。
再执行py脚本,这个不再提示No module named MySQLdb错误了,而是libmysqlclient.so.18文件找不到
[root@mvxl0783 sh]# python 1.py
Traceback (most recent call last):
File "1.py", line 4, in <module>
import MySQLdb
File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
修改/etc/profile文件里,添加如下内容:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/apps/svr/mariadb10/lib:/usr/local/lib
并执行source /etc/profile
再执行python 1.py不再报错。