来源:https://www.cnblogs.com/s-seven/p/9105973.html

由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。

[root@VM_105_217_centos Python-3.6.2]# python
Python 2.7.5 (default, Aug  4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

输入Python命令,查看可以得知是Python2.7.5版本

输入

which python

可以查看位置,一般是位于/usr/bin/python目录下。

下面介绍安装Python3的方法

yum 安装

yum -y install python3

升级 pip 版本

python3 -m pip install --upgrade pip

编译安装

在 centos7 上通过 yum 安装 python3 会安装 python3.6.8 版本,要是我们需要其它版本,则需要自己编译安装

首先安装依赖包

yum -y groupinstall "Development tools"
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 libffi-devel

然后根据自己需求下载不同版本的Python3,我下载的是Python3.7.6

wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz

然后解压压缩包,进入该目录,安装Python3

tar -xvJf  Python-3.7.6.tar.xz
cd Python-3.7.6
./configure --prefix=/usr/local/python3
make && make install

最后创建软链接

ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

在命令行中输入python3测试


以下为修改为阿里云pip源

创建pip.config 文件


mkdir ~/.pip
vim ~/.pip/pip.conf

添加pip源
输入i进入编辑模式

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

esc 键退出编辑模式,输入:wq保存退出

结束


wget https://static.meixiangdao.cn/files/python3.sh && sh python3.sh 
×