CentOS7 升级python同时解决yum损坏问题

时间:2021-08-19 09:19:05

CentOS7中的python版本为python2.7.5,升级到最新版的python时需要注意两个问题

  • 新版的python安装好后要修改python的系统默认指向问题
  • 升级到最新版python后yum报错的问题

下面对新版的安装步骤进行说明。

 一、下载并安装最新版python

  1.下载并解压

# wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
# tar -zxf Python-3.5..tgz

  2.安装Python

# cd Python-3.5./# ./configure 
# make
# make install 查看是否安装成功 # /usr/local/bin/python3.5
Python 3.5.2 (default, Jul 24 2016, 14:46:50)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

  3.修改python的系统默认指向,修改软链接

  正常情况下,新版本的python安装后系统等其他应用指向的python环境依然是老版本的python,

  如果删除老版本后再装新版本会面临很多难以定位的麻烦,所以通常的做法是:

  老版本依然保留,新版本并行存在。操作方法如下

()修改系统默认的python修改为2..5版本

# mv /usr/bin/python /usr/bin/python2.7.5

()创建软连接使python指向新版本

# ln -s /usr/local/bin/python3. /usr/bin/python

()检测是否更新成功

[root@localhost ~]# python
Python 3.5. (default, Jul , ::)
[GCC 4.8. (Red Hat 4.8.-)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

 二、yum报错问题解决

  1.终端下输入yum后报错信息如下

[root@localhost ~]# yum
File "/usr/bin/yum", line
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax

  2.vim /usr/bin/yum

   修改python头部python为python2.7.5

#!/usr/bin/python2.7.5
import sys
try:
import yum
except ImportError:
print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

  3.再次查看yum是否可用

[root@localhost ~]# yum
Loaded plugins: fastestmirror, langpacks
You need to give some command
Usage: yum [options] COMMAND List of Commands: check Check for problems in the rpmdb
check-update Check for available package updates
clean Remove cached data
deplist List a package's dependencies

... ...