CentOS 6.x系统升级Python到2.7版本的Shell脚本分享

 更新时间:2014年11月28日 09:28:40   投稿:junjie  
这篇文章主要介绍了CentOS 6.x系统升级Python到2.7版本的Shell脚本分享,本文把升级过程总结成一个自动脚本,需要的朋友可以参考下

在CentOS 6.x上,默认自带的Python是2.6.x版本,这个版本的Python有点老了,比如“collections.OrderedDict”就是2.7才有的,而且著名的Python Web框架Django的新版(如:1.7)就不支持Python2.6,最低要求是2.7了。而一些公司或者共有云上的服务器就是使用CentOS6.x,所以也就有了升级Python到2.7的需求。

升级Python之前,需要先安装一些工具和软件库,否则后面安装Python或pip时可能出错。
Python2.7通过源码安装,python可行执行程序默认是安装在/usr/local/bin/,一般来在$PATH中,/usr/local/bin是优先使用的(如果不是,需要自己设置一下PATH环境变量)。
在安装完python后,还需要安装easy_install和pip这两个最常用工具。

整个安装过程,我总结过如下一个Shell脚本(以安装Python2.7.8为例,以root权限运行),供参考:

https://github.com/smilejay/shell/blob/master/sh2014/install_py27_on_centos.sh

复制代码 代码如下:

#!/bin/bash
# a script to install python 2.7 on CentOS 6.x system.
# CentOS 6.x has python 2.6 by default, while some software (e.g. django1.7)
# need python 2.7.
 
# install some necessary tools & libs
echo "install some necessary tools & libs"
yum groupinstall "Development tools"
yum install openssl-devel zlib-devel ncurses-devel bzip2-devel readline-devel
yum install libtool-ltdl-devel sqlite-devel tk-devel tcl-devel
sleep 5
 
# download and install python
version='2.7.8'
python_url="https://www.python.org/ftp/python/$version/Python-${version}.tgz"
 
# check current python version
echo "before installation, your python version is: $(python -V &2>1)"
python -V 2>&1 | grep "$version"
if [ $? -eq 0 ]; then
  echo "current version is the same as this installation."
  echo "Quit as no need to install."
  exit 0
fi
 
echo "download/build/install your python"
cd /tmp
wget $python_url
tar -zxf Python-${version}.tgz
cd Python-${version}
./configure
make -j 4
make install
sleep 5
 
echo "check your installed python"
python -V 2>&1 | grep "$version"
if [ $? -ne 0 ]; then
  echo "python -V is not your installed version"
  /usr/local/bin/python -V 2>&1 | grep "$version"
  if [ $? -ne 0 ]; then
    echo "installation failed. use '/usr/local/bin/python -V' to have a check"
  fi
  exit 1
fi
sleep 5
 
# install setuptools
echo "install setuptools"
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
python ez_setup.py
# check easy_install version
easy_install --version
sleep 5
 
# install pip for the new python
echo "install pip for the new python"
easy_install pip
# check pip version
pip -V
 
echo "Finished. Well done!"
echo "If 'python -V' still shows the old version, you may need to re-login."
echo "And/or set /usr/local/bin in the front of your PATH environment variable."
echo "-------------------------"

相关文章

  • shell中if test使用详解

    shell中if test使用详解

    本文主要介绍了shell中if test使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Shell脚本case语句简明教程

    Shell脚本case语句简明教程

    这篇文章主要介绍了Shell脚本case语句简明教程,非常容易看懂的一篇文章,需要的朋友可以参考下
    2014-07-07
  • Shell正则表达式学习笔记

    Shell正则表达式学习笔记

    正则表达式是一种表示方式,可以让你查找匹配特定的准则的文本,例如以字母a开头的文件,学好正则表达式将使以后的文本处理变得方便,快速,本文给大家介绍shell正则表达式学习笔记,对shell正则表达式相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • Linux下Oracle归档日志自动清理脚本代码(sh)

    Linux下Oracle归档日志自动清理脚本代码(sh)

    将以下代码保存为.sh文件,并放到crontab里就可以实现自动清理
    2013-01-01
  • Shell脚本实现监控kingate并自动启动

    Shell脚本实现监控kingate并自动启动

    这篇文章主要介绍了Shell脚本实现监控kingate并自动启动,本文直接给出实现代码,需要的朋友可以参考下
    2014-12-12
  • 使用Shell遍历目录及其子目录中的所有文件方法

    使用Shell遍历目录及其子目录中的所有文件方法

    今天小编就为大家分享一篇使用Shell遍历目录及其子目录中的所有文件方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • shell脚本自动检测网络掉线和自动重连

    shell脚本自动检测网络掉线和自动重连

    这篇文章主要介绍了shell脚本自动检测网络掉线和自动重连,这篇文章介绍的是自动检测连接网络的解决方法,需要的朋友可以参考下
    2019-12-12
  • Linux Shell脚本实现检测tomcat

    Linux Shell脚本实现检测tomcat

    这篇文章主要介绍了Linux Shell脚本实现检测tomcat的方法,推荐给小伙伴们,需要的朋友可以参考下
    2015-03-03
  • shell通过正则匹配ip地址实例代码

    shell通过正则匹配ip地址实例代码

    这篇文章主要给大家介绍了关于shell如何通过正则匹配ip地址的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用shell具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • shell脚本去重的三种方法小结

    shell脚本去重的三种方法小结

    在实际工作中,进行Shell去重操作是非常常见的任务之一,本文主要介绍了shell脚本去重的三种方法小结,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12

最新评论