1、当前系统python版本

[root@localhost ~]# python -V
Python 2.7.5

2、下载所需python版本

[root@localhost ~]# wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz

3、解压

[root@localhost ~]# tar -xf Python-3.9.9.tgz -C /usr/local/

4、编译并安装 (编译时如果报错就是缺少某些依赖,直接yum安装就可以)

[root@localhost ~]#  cd /usr/local/Python-3.9.9/
[root@localhost Python-3.9.9]#  ls
[root@localhost Python-3.9.9]#  ./configure 
[root@localhost Python-3.9.9]#  make && make install

5、完成后,现在系统中有两个版本的python

[root@localhost Python-3.9.9]# python -V
Python 2.7.5
[root@localhost Python-3.9.9]# python3 -V
Python 3.9.9

6、查看python命令

[root@localhost Python-3.9.9]# ll /usr/bin/ |grep python
-rwxr-xr-x.   1 root root       11312 Nov 14  2018 abrt-action-analyze-python
lrwxrwxrwx    1 root root           9 Apr  7 15:53 python2 -> python2.7
-rwxr-xr-x    1 root root        7144 Nov 17  2020 python2.7
lrwxrwxrwx    1 root root           7 Apr  7 15:53 python -> python2

7、python3替换python

[root@localhost Python-3.9.9]#  mv /usr/bin/python /usr/bin/python.bak
[root@localhost Python-3.9.9]#  ll /usr/bin/ |grep python
		-rwxr-xr-x.   1 root root       11312 Nov 14  2018 abrt-action-analyze-python
		lrwxrwxrwx    1 root root          22 Apr 15 11:11 python -> /usr/local/bin/python3
		lrwxrwxrwx    1 root root           9 Apr  7 15:53 python2 -> python2.7
		-rwxr-xr-x    1 root root        7144 Nov 17  2020 python2.7
		lrwxrwxrwx    1 root root           7 Apr  7 15:53 python.bak -> python2

[root@localhost Python-3.9.9]#  which python3
		/usr/local/bin/python3
[root@localhost Python-3.9.9]#  ln -s /usr/local/bin/python3 /usr/bin/python
[root@localhost Python-3.9.9]#  python -V
		Python 3.9.9

8、python升级后会影响yum firewall-cmd 命令,修改下面目录中首行#!/usr/bin/python 为#!/usr/bin/python2.7 (原系统版本)

/usr/bin/yum    /usr/libexec/urlgrabber-ext-down     /usr/sbin/firewalld     /usr/bin/firewall-cm

9、备注:
如果配置编译的时候遇到以下问题时./configure不加 –enable-optimizations参数

SystemError: <built-in function compile> returned NULL without setting an error
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] Error 1
make[1]: Leaving directory `/usr/local/src/Python-3.8.0'
make: *** [profile-opt] Error 2
Logo

更多推荐