linux一键安装Python3脚本

linux 系统很多默认安装的是 python2,例如 centos 就是默认安装的python2,但是有的程序必须要python3,这时候,我们就需要安装一个python3,同时将系统中,依赖python2的程序改一下,如yum是必须依赖与python2的,就需要将yum的执行器改为python2。


一键安装python3

直接复制到您的linux命令行,就能安装python3了。

wget http://66-ai.com/download/script-litte-prince/app/install-python3.sh -O /root/install-python3.sh  && sh install-python3.sh 

上面的代码是自动安装,如果想看看install-python3.sh 脚本中是什么,可以看下面的脚本:

#!/usr/bin/env bash
# 安装python3
# 自动将yum的命令依赖改为python2,以免引起yum异常

yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

if ! [ -x "$(command -v axel)" ]; then
  wget http://66-ai.com/download/script-litte-prince/Python-3.6.5.tgz -O /root/Python-3.6.5.tgz
else
  axel -n 10 -a http://66-ai.com/download/script-litte-prince/Python-3.6.5.tgz -o /root/Python-3.6.5.tgz
fi

tar -xzvf Python-3.6.5.tgz
cd Python-3.6.5
./configure --prefix=/usr/local/python
make
make install
cd /usr/bin
mv python python.bak
mv pip pip.bak
ln -s /usr/local/python/bin/python3.6 /usr/bin/python
ln -s /usr/local/python/bin/pip3.6 /usr/bin/pip


python2str=`cat /usr/bin/yum | grep -w '#!/usr/bin/python2'`

if [ $python2str = '#!/usr/bin/python2' ]; then
    echo '已经替换过'
else
    sed -i 's@#!/usr/bin/python@#!/usr/bin/python2@g' /usr/bin/yum
    sed -i 's@#!/usr/bin/python@#!/usr/bin/python2@g' /usr/bin/yum-builddep
    sed -i 's@#!/usr/bin/python@#!/usr/bin/python2@g' /usr/bin/yum-config-manager
    sed -i 's@#!/usr/bin/python@#!/usr/bin/python2@g' /usr/bin/yum-debug-dump
    sed -i 's@#!/usr/bin/python@#!/usr/bin/python2@g' /usr/bin/yum-debug-restore
    sed -i 's@#!/usr/bin/python@#!/usr/bin/python2@g' /usr/bin/yumdownloader
    sed -i 's@#!/usr/bin/python@#!/usr/bin/python2@g' /usr/bin/yum-groups-manager
fi

python2和python3共存

上面安装脚本中,我们没有卸载python2,如果你想用python2,那么就用python2命令,如果你想用python3就用python命令。

这样就做到了python2和python3共存了。