码迷,mamicode.com
首页 > 编程语言 > 详细

CentOS 升级 Python3 (附带: 一键升级脚本)

时间:2017-10-27 13:32:43      阅读:516      评论:0      收藏:0      [点我收藏+]

标签:网上   --   import   安装失败   stat   /usr   form   dir   ati   

 

  升级环境
 
应用名称
版本
Python
3.5.2
Syatem
CentOS 6.7
 
 
 
  升级方法
 
【1】下载 Python 3:
1
wget http://mirrors.sohu.com/python/3.5.2/Python-3.5.2.tgz
 
 
【2】安装依赖
1
2
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel
yum -y install sqlite-devel readline-devel tk-devel gcc make
 
 
【3】编译安装 python3
1
2
3
4
5
6
7
8
9
tar -xzf Python-3.5.2.tgz

cd Python-3.5.2

./configure --prefix=/usr/local/python3 --enable-shared CFLAGS=-fPIC
 
make -4
 
make install
 
 
【4】修改环境变量
1
2
3
4
vim /etc/profile

添加
export PATH=$PATH:/usr/local/python3/bin
 
 
【5】修改原有的 python
1
2
mv /usr/bin/python /usr/bin/python2.6
ln -/usr/local/python3/bin/python3.5 /usr/bin/python
 
 
【6】修改 lib 文件
1
2
3
4
5
查看库文件,如果那个问题,就find找到,然后拷贝到 /lib64 

ldd /usr/local/python3/bin/python3

cp /usr/local/python3/lib/libpython3.5m.so.1.0 /lib64/
 
 
【7】修改 yum
1
sed -"s#/usr/bin/python#/usr/bin/python2.6#g" /usr/bin/yum
 
 
【8】生效检查
1
2
3
source /etc/profile

python -v
 
 
 
  一键安装脚本(附带)
 
#!/bin/bash

######################################
##                                  ##
##  vers:    1.0                    ##
##  author:  Dylan                  ##
##  date:    20171027               ##
##  useage: Update Python to 3.5   ##
##                                  ##
######################################


#定义颜色输出
Color_Text() {
  echo -e " \e[0;$2m$1\e[0m"
}

Echo_Red() {
  echo $(Color_Text "$1" "31")
}

Echo_Green() {
  echo $(Color_Text "$1" "32")
}

Echo_Yellow() {
  echo $(Color_Text "$1" "33")
}


# Python 版本
Python_Version=3.5.2

# Python 安装包
Python_Package="Python-${Python_Version}.tgz"

# Python 下载地址
Python_Url="http://mirrors.sohu.com/python/${Python_Version}/${Python_Package}"

# 包存放地址
Package_Dir=$(pwd)

# 安装目录
Python_Install_Dir="/usr/local/python-${Python_Version}"


# 网络检查
NETWORK_CHECK() {

    # 检查联网情况
    ping www.baidu.com -c 3

    if [ $? != 0 ];then
        Echo_Red "该服务器无法连网,请配置服务器网络!"
        exit 1
    fi
    
}


# 升级 Python
PYTHON_UPDATE() {

    # 检查网络
    NETWORK_CHECK

    # 安装依赖
    Echo_Green 安装依赖:
    yum -y install zlib-devel bzip2-devel wget openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

    # 检查 Python 版本
    Echo_Green 当前版本 Python:
    Echo_Yellow "==========================================================================="
    /usr/bin/python -V
    Echo_Yellow "==========================================================================="


    # 确认升级
    read -p "是否继续升级 Python [y/n]:" Chose_Number
    case ${Chose_Number} in
        [yY][eE][sS]|[yY])
            Echo_Yellow "你选择的是yes,升级继续进行..."
        ;;
        [nN][oO]|[nN])
            Echo_Yellow "你选择的是yes,升级即将终止..."
            exit
        ;;
        *)
            Echo_Red "输入错误,即将退出升级..."
            exit
    esac

    # 检测包是否存在
    ls -l ${Package_Dir}/${Python_Package}

    if [ $? != 0 ];then
        # 下载安装包
        Echo_Yellow "开始从网上下载 ${Python_Package}..."
        wget ${Python_Url}
        if [ $? != 0 ];then
            Echo_Red "网上下载 ${Python_Package} 失败,请检查!"
            exit 1
        fi
        Echo_Yellow "${Python_Package} 下载成功,即将开始升级..."
    fi

    # 解压安装
    Echo_Yellow "开始解压 Python ..."
    tar -zxf ${Python_Package} && cd Python-${Python_Version}

    if [ $? != 0 ];then
        Echo_Red "${Python_Package} 解压失败,请检查!"
        exit 1
    fi

    Echo_Yellow "开始配置 Python ..."
    ./configure --prefix=${Python_Install_Dir} --enable-shared CFLAGS=-fPIC

    if [ $? != 0 ];then
        Echo_Red "${Python_Package} 解压失败,请检查!"
        exit 1
    fi

    Echo_Yellow "开始编译 Python ..."
    make -j 4

    if [ $? != 0 ];then
        Echo_Red "${Python_Package} 编译失败,请检查!"
        exit 1
    fi

    Echo_Yellow "开始安装 Python ..."
    make install

    if [ $? != 0 ];then
        Echo_Red "${Python_Package} 安装失败,请检查!"
        exit 1
    fi

    # 配置环境变量
    Echo_Yellow "修改环境变量 ..."
    echo "export PATH=\$PATH:${Python_Install_Dir}/bin" >>/etc/profile

    # 修改原有的 Python 为新的
    Echo_Yellow "替换旧版 Python ..."
    mv /usr/bin/python /tmp
    ln -s ${Python_Install_Dir}/bin/python3 /usr/bin/python

    # 修改库文件
    Echo_Yellow "修改 Python 库文件 ..."
    ldd ${Python_Install_Dir}/bin/python3
    cp ${Python_Install_Dir}/lib/libpython3.5m.so.1.0 /lib64/

    # 修改 yum 配置
    Echo_Yellow "修改 yum 文件 ..."
    sed -i "s#/usr/bin/python#/usr/bin/python2.6#g" /usr/bin/yum

    # 使配置生效
    source /etc/profile

    # 查看升级后版本
    Echo_Green 当前版本 Python(建议手动执行:source /etc/profile):
    Echo_Yellow "==========================================================================="
    /usr/bin/python -V
    Echo_Yellow "==========================================================================="
}


# 输出安装信息
Echo_Yellow "==========================================================================="
Echo_Green ‘‘
Echo_Green 版本:1.0
Echo_Green 作者:Dylan
Echo_Green 日期:20171027
Echo_Green 备注:详情可以联系QQ:1214966109
Echo_Green ‘‘
Echo_Yellow "==========================================================================="
Echo_Green ‘‘
Echo_Yellow 升级即将开始...
Echo_Green ‘‘
sleep 2

# 升级
PYTHON_UPDATE

 

 
 
 
 

 

CentOS 升级 Python3 (附带: 一键升级脚本)

标签:网上   --   import   安装失败   stat   /usr   form   dir   ati   

原文地址:http://www.cnblogs.com/P1no/p/7742404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!