生产环境中,经常会用到 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!