CentOS7下安装Python3及Pip3并保留Python2

安装依赖环境
1
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
浏览器打开 https://www.python.org/ftp/python/ 查看最新的Python版本,标记为3.A.B
1
wget https://www.python.org/ftp/python/3.A.B/Python-3.A.B.tgz
创建Python3的目录
1
mkdir /usr/local/python3
解压下载文件并切换目录
1
2
tar -zxvf Python-3.A.B.tgz
cd Python-3.A.B
执行
1
2
./configure --prefix=/usr/local/python3
make && make install
创建Python3的软链接
1
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
创建Pip3的软链接
1
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
测试命令 python3 和 pip3
1
2
python3
pip3
因为执行yum需要python2版本,所以我们还要修改yum的配置,执行:

vim /usr/bin/yum

把#! /usr/bin/python修改为#! /usr/bin/python2

同理 vim /usr/libexec/urlgrabber-ext-down

文件里面的#! /usr/bin/python 也要修改为#! /usr/bin/python2

安装保留了原python命令调用python2

安装完成

ps 安装报错
1
2
3
4
5
6
7
8
9
10
Q:
ModuleNotFoundError: No module named '_ctypes'

A:
yum -y install gcc gcc-c++
yum -y install zlib zlib-devel
yum -y install libffi-devel

Q:configure: error: no acceptable C compiler found in $PATH
A:yum install gcc