注意:以下内容均使用root用户执行操作。
1-确认信息
# uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
#
# cat /etc/system-release
CentOS Linux release 7.5.1804 (Core)
#
# python -V
Python 2.7.5
# python2 -V
Python 2.7.5
#
# ll /usr/bin |grep python
-rwxr-xr-x. 1 root root 7280 Jan 30 2018 pmpython
lrwxrwxrwx. 1 root root 7 Jul 25 12:25 python -> python2
lrwxrwxrwx. 1 root root 9 Jul 25 12:25 python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 May 31 2018 python2.7
2-安装依赖
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc libffi-devel
3-下载与安装
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar -zxf Python-3.7.4.tgz
cd Python-3.7.4/
./configure --prefix=/usr/local/python3.7
make && make install
4-更改软连接
ln -s /usr/local/python3.7/bin/python3 /usr/bin/python3
rm -rf /usr/bin/python
ln -s /usr/local/python3.7/bin/python3 /usr/bin/python
ln -s /usr/local/python3.7/bin/pip3 /usr/bin/pip
5-修改yum相关配置
为了防止出现如下错误:
# yumFile "/bin/yum", line 30except KeyboardInterrupt, e:^
SyntaxError: invalid syntax
如下文件中的#!/usr/bin/python
更改为#!/usr/bin/python2
- /usr/bin/yum
- /usr/libexec/urlgrabber-ext-down
- /usr/bin/yum-config-manager
通过sed命令一次性修改多个文件
# sed -i 's_#!/usr/bin/python_#!/usr/bin/python2_' /usr/bin/yum /usr/libexec/urlgrabber-ext-down /usr/bin/yum-config-manager
6-验证
# python3 -V
Python 3.7.4
# python -V
Python 3.7.4
# pip -V
pip 19.0.3 from /usr/local/python3.7/lib/python3.7/site-packages/pip (python 3.7)
#
# ll /usr/bin |grep python
lrwxrwxrwx 1 root root 29 Jul 29 10:14 pip -> /usr/local/python3.7/bin/pip3
-rwxr-xr-x. 1 root root 7280 Apr 13 2018 pmpython
lrwxrwxrwx 1 root root 32 Jul 29 10:14 python -> /usr/local/python3.7/bin/python3
lrwxrwxrwx. 1 root root 9 Nov 16 2018 python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Apr 11 2018 python2.7
lrwxrwxrwx 1 root root 32 Jul 29 10:14 python3 -> /usr/local/python3.7/bin/pytho3
#