Centos7.6 下 Python2.7 升级为 Python3.7

生产环境中,经常会用到 python 环境,而且很多是 python 3 的环境,但是 centos 7 中,自带的 python 环境是 python 2.7 的,不符合,所以需要进行升级。

准备环境:Centos 7

# 关闭防火墙、修改 seLinux 为警告模式
[root@fp-21 ~]# systemctl stop firewalld.service 
[root@fp-21 ~]# setenforce 0

查看当前 python 版本

[root@fp-21 ~]# python -V
Python 2.7.5

配置 yum 源

# 这里使用的是阿里云的源
[root@fp-21 ~]# cd /etc/yum.repos.d/
[root@fp-21 yum.repos.d]# mkdir repo.bak
[root@fp-21 yum.repos.d]# mv *.repo repo.bak/
[root@fp-21 yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
……
[root@fp-21 yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-7.repo
……
[root@fp-21 yum.repos.d]# yum clean all
[root@fp-21 yum.repos.d]# yum makecache

安装所需依赖

[root@fp-21 ~]# yum -y install sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel zlib-devel bzip2-devel openssl-devel openssl-static ncurses-devel lzma gcc
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
 ……
 Complete!
 
[root@fp-21 ~]# yum -y groupinstall "Development tools"
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
……

下载 python3.7 安装包并解压

[root@fp-21 src]# cd /opt/
[root@fp-21 opt]# mkdir python
[root@fp-21 opt]# cd python/
[root@fp-21 opt]# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
……
[root@fp-21 python]# ls
Python-3.7.3.tar.xz
[root@fp-21 python]# tar -xf Python-3.7.3.tar.xz 
[root@fp-21 python]# tar -xf Python-3.7.3.tar.xz -C /usr/local/

开始安装

[root@fp-21 Python-3.7.3]# ./configure --prefix=/usr/local/python3
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
……
[root@fp-21 Python-3.7.3]# make && make install
Installing collected packages: setuptools, pip
Successfully installed pip-19.0.3 setuptools-40.8.0

修改二进制命令路径

[root@fp-21 Python-3.7.3]# cd /usr/bin/
[root@fp-21 bin]# ll |grep python
-rwxr-xr-x.   1 root root      11312 Nov 14  2018 abrt-action-analyze-python
lrwxrwxrwx.   1 root root          7 Nov 21 23:34 python -> python2
lrwxrwxrwx.   1 root root          9 Nov 21 23:34 python2 -> python2.7
-rwxr-xr-x.   1 root root       7216 Oct 31  2018 python2.7
[root@fp-21 bin]# rm -rf python
[root@fp-21 bin]# ln -s /usr/local/python3/bin/python3 ./python
[root@fp-21 bin]# ll |grep python
-rwxr-xr-x.   1 root root      11312 Nov 14  2018 abrt-action-analyze-python
lrwxrwxrwx.   1 root root         30 Mar 11 06:04 python -> /usr/local/python3/bin/python3
lrwxrwxrwx.   1 root root          9 Nov 21 23:34 python2 -> python2.7
-rwxr-xr-x.   1 root root       7216 Oct 31  2018 python2.7

查看 python 的版本

[root@fp-21 bin]# python -V
Python 3.7.3

对 pip 进行指定

[root@fp-21 bin]# ln -s /usr/local/python3/bin/pip3 ./pip
[root@fp-21 bin]# pip -V
pip 19.0.3 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)

这里还有一个注意点

[root@fp-21 ~]# yum -y install sl
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

发现 yum 用不了,这里需要修改几个地方

# 第一个
[root@fp-21 ~]# vim /usr/bin/yum
  1 #!/usr/bin/python2.7
  
# 第二个
[root@fp-21 ~]# vim /usr/libexec/urlgrabber-ext-down
  1 #! /usr/bin/python2.7

此时,再去使用 yum 就可以了

[root@fp-21 ~]# yum -y install sl
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
……
Installed:
  sl.x86_64 0:5.02-1.el7                                                                                                             
Complete!

顺便提一下,sl 命令,很有趣的命令,大家可以试试

在这里插入图片描述

只有注入思想的博客才是好的博客

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值