Centos系统一键升级python到2.7.9与3.7脚本

原文转载原创文章请注明,转载自: 沧水的博客 » Centos系统一键升级python到2.7.9与3.7脚本https://cangshui.net/?p=2198

centos6自带python是2.6,正巧需要给四五台机器升级python,于是干脆写了如下脚本

直接使用:

wget -N --no-check-certificate https://down.cangshui.net/-mysh/pythoninstall/centos6uppython.sh &&  chmod +x centos6uppython.sh && bash centos6uppython.sh


脚本预览

echo "请输入序号选择你要干啥:  
输入1. 升级到Python 2.7.9 
输入2. 安装python3,,并与Python2共存
输入99. 直接删除python2.7和python3__危险!有可能使Yum将无法使用,知道vi /usr/bin/yum  自便
"  
  
value=0;  
  
read -p "请输入选项: " value  
  
case $value in  
    1) echo "升级python2.7.9"  
sleep 1s
# install some necessary tools & libs
echo "install some necessary tools & libs"
yum -y groupinstall "Development tools"
yum -y install openssl-devel zlib-devel ncurses-devel bzip2-devel readline-devel
yum -y install libtool-ltdl-devel sqlite-devel tk-devel tcl-devel
yum install -y readline readline-devel readline-static
yum install -y openssl openssl-devel openssl-static
yum install -y bzip2-devel bzip2-libs
sleep 5

# download and install python
version='2.7.9'
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 --no-check-certificate $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 "安装 setuptools"
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
python ez_setup.py
# check easy_install version
easy_install --version
sleep 5

# install pip for the new python
echo "正在用pip安装新的 python"
easy_install pip
# check pip version
pip -V

echo "安装完成"
echo "如果 'python -V' 仍然显示旧版本,可能需要重新运行脚本"
echo "然后/或者在 set /usr/local/bin 设置环境变量"
echo "----------------------------------------------------------------------------------------------------------"
echo "有问题请在   https://cangshui.net/?p=2198  评论区留言,我会尽可能的回复(忙的话就要等很久了呢。。。"
echo "----------------------------------------------------------------------------------------------------------"
wget -P /home https://down.cangshui.net/-mysh/pythoninstall/pythonzijian.py
python /home/pythonzijian.py
rm -rf /home/pythonzijian.py
        ;;  
    2) echo "安装python3,,并与Python2共存"  
  sleep 1s
yum -y groupinstall 'Development Tools'
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel
sleep 2s
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0b3.tar.xz
tar Jxvf Python-3.7.0b3.tar.xz
cd Python-3.7.0b3
./configure --prefix=/usr/local/python3
make && make install
echo 'export PATH=$PATH:/usr/local/python3/bin' >> ~/.bashrc
ln -sv /usr/local/python3/bin/python3.7 /usr/bin/python3
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
echo "----------------------------------------------------------------------------------------------------------" 
echo "有问题请在 https://cangshui.net/?p=2198 评论区留言,我会尽可能的回复(忙的话就要等很久了呢。。。" 
echo "----------------------------------------------------------------------------------------------------------"
wget -P /home https://down.cangshui.net/-mysh/pythoninstall/pythonzijian.py
python3 /home/pythonzijian.py
rm -rf /home/pythonzijian.py
        ;;  
   99) echo "卸载!"  
echo "正在卸载,请等待"
rm -rf /usr/local/bin/python2.7
rm -rf /usr/local/bin/python2.7-config
rm -rf /usr/local/lib/python2.7
echo "删除完成"
        ;; 
    *) echo "请你输入选项"
        exit 1  
        ;;  
esac
转载原创文章请注明,转载自: 沧水的博客 » Centos系统一键升级python到2.7.9与3.7脚本