Centos 7.6安装python3.7

本文最后更新于:1 年前

Centos 7.6安装python3.7

保留原有python2更新pip

1
2
3
4
5
6
7
# 安装Python2的pip
yum install epel-release -y
yum -y install python-pip
# 安装完成后不是最新的pip版本要进行升级
pip install --upgrade pip
# 测试
pip -V(大写V) //pip 18.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)

若安装失败则手动更新

1
2
3
4
5
6
# 先卸载安装的python-pip
sudo yum remove python-pip

# 下载get-pip.py文件
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
sudo python get-pip.py

安装python3

安装依赖包

1
2
yum install libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
yum install libffi-devel -y

下载python3

1
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz //按需求修改版本号

解压

1
tar -xvJf Python-3.7.2.tar.xz

安装

1
2
3
cd Python-3.7.2
./configure prefix=/usr/local/python3
make && make install

安装完毕,/usr/local/目录下就会有python3了。

配置python3

1
2
3
4
5
6
7
8
9
10
#设置python3软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
#测试
python3 --version
#设置pip3软连接
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
#升级pip3
pip3 install --upgrade pip
#测试
pip3 -V

设置yum使用python2

1
2
vi /usr/bin/yum
#! /usr/bin/python 修改为 #! /usr/bin/python2