Python版本低引发Python脚本运行错误
系统平台:CentOS 6.3 x86_64
源由:
最近在部署nagios监控RabbitMQ,在MQ服务器运行如下命令出现如下提示:
[root@mq01 scripts]# ./check_rabbitmq -a connection_count -W 80 -C 100
CRITICAL - 'module' object has no attribute 'check_output'
原因:
Python版本太低
[root@mq01 scripts]# python -V
Python 2.6.6
解决方法:升级python
(1)安装python
[root@mq01 zjqiu]# wget http://www.python.org/ftp/python/2.7/Python-2.7.tar.bz2
[root@mq01 zjqiu]# tar jxf Python-2.7.tar.bz2
[root@mq01 zjqiu]# cd Python-2.7
[root@mq01 Python-2.7]# ./configure
[root@mq01 Python-2.7]# make && make install
python2.7安装后路径默认是在/usr/local/lib/python2.7
[root@mq01 Python-2.7]# ll /usr/local/lib/
total 9980
-r-xr-xr-x. 1 root root 10191084 Sep 5 06:20 libpython2.7.a
drwxr-xr-x. 2 root root 4096 Sep 5 06:22 pkgconfig
drwxr-xr-x. 28 root root 20480 Sep 5 06:22 python2.7
[root@mq01 Python-2.7]# /usr/local/bin/python -V
Python 2.7
[root@mq01 Python-2.7]# whereis python
python: /usr/bin/python /usr/bin/python2.6 /usr/bin/python /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python2.7 /usr/local/bin/python /usr/local/bin/python2.7-config /usr/local/lib/python2.7 /usr/include/python2.6 /usr/share/man/man1/python.1.gz
(2)建立软连接,使系统默认的python指向python2.7
正常情况下即使python2.7安装成功后,系统默认指向的python仍然是2.6.6版本
[root@mq01 Python-2.7]# mv /usr/bin/python /usr/bin/python.bak
[root@mq01 Python-2.7]# ln -s /usr/local/bin/python2.7 /usr/bin/python
(3)解决系统python软链接指向python2.7版本后,yum出错!
方法:
[root@mq01 Python-2.7]# vi /usr/bin/yum
将文本编辑显示的#/usr/bin/python修改为#/usr/bin/python2.7,保存修改即可!