CentOS python版本升级

写在前面

从2.x 到 3.x的升级
升级不代表完全把2.x移除,像yum这样的命令依赖2.x,所以最终结果是2.x和3.x共存,且3.x优先

Centos7 默认安装的是Python 2.x

[root@auto home]# python
Python 2.7.5 (default, Aug  7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Download Python 3.7 到Linux目录,比如/root

[root@auto home]# cd ~
[root@auto ~]# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz

解压

[root@auto ~]# tar -xf Python-3.7.3.tar.xz

安装编译所需依赖

[root@auto ~]# yum -y install bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel

进入解压目录编译并安装

[root@auto ~]# cd Python-3.7.3
[root@auto Python-3.7.3]# ./configure --prefix=/usr/local/python3.7 --with-ssl # pip3会用到ssl模块,之前没有指定所以该功能不能使用
[root@auto Python-3.7.3]# make && make install

检查下安装过程有没有错误

[root@auto Python-3.7.3]# echo $?
0    # 回显是 0 ,证明没错

备份Python 2

[root@auto Python-3.7.3]# mv /usr/bin/python /usr/bin/python2.bak

创建软连接,把Python 3设成默认

[root@auto Python-3.7.3]# ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python

验证成功与否

[root@auto Python-3.7.3]# python
Python 3.7.3 (default, Feb 19 2020, 21:48:52)
...

因yum要使用python2,将/usr/bin/python改为python3后,yum就不能正常运行了,需要改下配置

[root@auto Python-3.7.3]# sed -i 's/python/python2/g' /usr/bin/yum
[root@auto Python-3.7.3]# sed -i 's/python/python2/g' /usr/libexec/urlgrabber-ext-down
posted @ 2020-10-09 14:36  z417  阅读(196)  评论(0编辑  收藏  举报