如果你的python已经升级到了 3.x 请忽略。
1、进入python官网(https://www.python.org),选择需要的版本。此处我选择当前最新版本Python3.8
[root@localhost ~ xxx] yum install gcc gcc-c++ -y
[root@localhost ~ xxx] wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tar.xz
[root@localhost ~ xxx] tar xvf Python-3.8.0.tar.xz
[root@localhost ~ xxx] cd Python-3.8.0
[root@localhost ~ xxx]./configure
[root@localhost ~ xxx] make
[root@localhost ~ xxx] make install
---------- 安装出错 执行----------
错误1:ModuleNotFoundError: No module named ‘zlib’,则执行以下:
[root@localhost ~ xxx] yum install zlib zlib-devel openssl
错误2:make 期间如现以下错误:
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
升级到LibreSSL,参考如下:
https://blog.csdn.net/Scorpio921/article/details/82682757
不管是哪个错误 ,都要进入python3.8.0目录后在重新编译
[root@localhost ~ xxx] ./configure
[root@localhost ~ xxx] make
[root@localhost ~ xxx] make install
---------- 安装出错 执行----------
最后看到:Successfully installed pip-19.2.3 setuptools-41.2.0 才算安装成功!
2、验证
[root@localhost ~ xxx] python -V #一个是旧版本,一个是新版本
Python 2.7.5
[root@localhost ~ xxx] python3 -V
Python 3.8
3、设置3.x为默认版本
[root@localhost ~ xxx] mv /usr/bin/python /usr/bin/python.bak
#将原来 python 的软链接重命名
[root@localhost ~ xxx] ln -s /usr/local/bin/python3 /usr/bin/python
#将 python 链接至 python3,第一个参数可以用whereis python查看
4、配置yum
升级 Python 之后,由于将默认的 python 指向了 python3,yum 不能正常使用.
修改/usr/bin/yum和/usr/libexec/urlgrabber-ext-down,将 #!/usr/bin/python 改为 #!/usr/bin/python2.7,保存退出即可。
[root@localhost ~ xxx] vi /usr/bin/yum
[root@localhost ~ xxx] vi /usr/libexec/urlgrabber-ext-down