【运维】Linux升级Python2
- 操作系统:centos7
- python版本:升级前版本2.7.5,升级后2.7.18
- python3版本:升级前3.6.8,升级后版本3.8.8
1、查看当前python版本
[root@1 ~]# python -V
Python 2.7.5
2、yum安装python
[root@s143 ~]# yum install -y python2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package python-2.7.5-90.el7.x86_64 already installed and latest version
Nothing to do
python2升级步骤
0、查找需要升级的版本
python官网(包括python和python3所有的版本)查找需要安装或者升级的版本。
找到需要的Python版本
Index of /ftp/python/
1、下载python新版本
[root@s143 opt]# wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
2、解压软件包
[root@s143 opt]# tar -zxvf Python-2.7.18.tgz
3、预编译软件包
[root@s143 opt]# cd Python-2.7.18
[root@s143 Python-2.7.18]# ./configure --prefix=/usr
4、编译软件包
[root@s143 Python-2.7.18]# make
…
running build_scripts
creating build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/pydoc -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/idle -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/2to3 -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Lib/smtpd.py -> build/scripts-2.7
changing mode of build/scripts-2.7/pydoc from 644 to 755
changing mode of build/scripts-2.7/idle from 644 to 755
changing mode of build/scripts-2.7/2to3 from 644 to 755
changing mode of build/scripts-2.7/smtpd.py from 644 to 755
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
5、安装软件包
[root@s143 Python-2.7.18]# make install
6、检查升级后的版本
[root@s143 Python-2.7.18]# python -V
Python 2.7.18
问题
升级到python2.7.18 yum 报错No module named rpm
报错信息:There was a problem importing one of the Python modules required to run yum. The error leading to this problem was:
No module named rpm
- 报错原因:python2.7.18是python2的最新版本,已经移除了对yum的支持,centos8默认使用dnf软件包安装工具。
- 解决方案:从其他centos7服务器拷贝一个python2.7.5到服务器/usr/bin目录下,然后重建python软链接。
或者直接将user/bin/yum 编辑为以下