Linux--升级Python3

1、确定Python版本

[root@izj6cb2khkpzmfo4s7nbl4z ~]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

查看目前系统使用的Python以及安装目录,接下来将要对该目录下的Python进行修改

[root@izj6cb2khkpzmfo4s7nbl4z ~]# which python
/usr/bin/python
[root@izj6cb2khkpzmfo4s7nbl4z ~]# ll /usr/bin/python*
lrwxrwxrwx 1 root root    7 Dec  5 18:01 /usr/bin/python -> python2
lrwxrwxrwx 1 root root    9 Dec  5 18:01 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Oct 31 07:46 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 Oct 31 07:46 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root   16 Dec  5 18:01 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root   14 Dec  5 18:01 /usr/bin/python-config -> python2-config

2、下载安装

通过wget从官网获取安装包

wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

解压缩

tar -zxvf Python-3.6.5.tgz

在解压目录下安装配置

# cd Python-3.6.5
# ./configure

编译并安装

# make
# make install

查看一下新安装的python3的文件位置

[root@izj6cb2khkpzmfo4s7nbl4z Python-3.6.5]# ll /usr/local/bin/python*
lrwxrwxrwx 1 root root        9 Dec 15 09:44 /usr/local/bin/python3 -> python3.6
-rwxr-xr-x 2 root root 12705608 Dec 15 09:44 /usr/local/bin/python3.6
lrwxrwxrwx 1 root root       17 Dec 15 09:44 /usr/local/bin/python3.6-config -> python3.6m-config
-rwxr-xr-x 2 root root 12705608 Dec 15 09:44 /usr/local/bin/python3.6m
-rwxr-xr-x 1 root root     3117 Dec 15 09:44 /usr/local/bin/python3.6m-config
lrwxrwxrwx 1 root root       16 Dec 15 09:44 /usr/local/bin/python3-config -> python3.6-config

3、替换旧的Python

已经确认当前使用Python为2.7.5并且在/usr/bin目录下,接下来需要做的就是替换/usr/bin/python,并更改yum使用的解释器,不然yum将无法使用

1、重命名当前Python

[root@izj6cb2khkpzmfo4s7nbl4z Python-3.6.5]# mv /usr/bin/python /usr/bin/old_python
[root@izj6cb2khkpzmfo4s7nbl4z Python-3.6.5]# ll /usr/bin/ | grep python
lrwxrwxrwx    1 root root         7 Dec  5 18:01 old_python -> python2
lrwxrwxrwx    1 root root         9 Dec  5 18:01 python2 -> python2.7
-rwxr-xr-x    1 root root      7216 Oct 31 07:46 python2.7
-rwxr-xr-x    1 root root      1835 Oct 31 07:46 python2.7-config
lrwxrwxrwx    1 root root        16 Dec  5 18:01 python2-config -> python2.7-config
lrwxrwxrwx    1 root root        14 Dec  5 18:01 python-config -> python2-config

2、新建Python3.6的软连接到/usr/bin/python,建好后查看已经存在,并指向正确
链接出错:
在这里插入图片描述
创建、删除、修改软连接
根据以上教程,删除错误的软连接,再重新创建软连接,就好了
在这里插入图片描述

[root@izj6cb2khkpzmfo4s7nbl4z bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python
[root@izj6cb2khkpzmfo4s7nbl4z bin]# ll /usr/bin/python
lrwxrwxrwx 1 root root 24 Dec 15 10:10 /usr/bin/python -> /usr/local/bin/python3.6
[root@izj6cb2khkpzmfo4s7nbl4z bin]# python
Python 3.6.5 (default, Dec 15 2018, 09:42:55) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

由以上信息可知,升级成功了!

4、解决升级Python后yum,Firewall等不能用的问题

1、 升级Python后运行yum会报错

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

vim /usr/bin/yum 编辑yum的执行文件,更改首行的/usr/bin/python 为上面重命名的名字(/usr/bin/old_python),之后再运行yum就正常了
在这里插入图片描述

在这里插入图片描述

2、 运行yum安装其他软件可能出现以下错误,这也是因为Python版本问题,
vim /usr/libexec/urlgrabber-ext-down 修改第一行的Python为重命名的名字(指向python2.7的 /usr/bin/old_python)

在这里插入图片描述

3、防火墙无法启动
和上面一样,修改如下文件 vim /usr/sbin/firewalld
在这里插入图片描述

修改之后,打开防火墙就成功了!

[root@izj6cb2khkpzmfo4s7nbl4z bin]# vim /usr/sbin/firewalld
[root@izj6cb2khkpzmfo4s7nbl4z bin]# systemctl start firewalld.service
[root@izj6cb2khkpzmfo4s7nbl4z bin]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-12-15 10:30:56 CST; 26s ago
     Docs: man:firewalld(1)
 Main PID: 27378 (firewalld)

除此之外还有一些其他文件使用的Python,可以使用grep命令查看哪些文件使用了Python解释器,根据具体情况修改。例如查看/usr/bin目录下的文件

[root@izj6cb2khkpzmfo4s7nbl4z bin]# grep -ai /usr/bin/python /usr/bin/*
/usr/bin/chardetect:#!/usr/bin/python
/usr/bin/cheetah:#!/usr/bin/python2
/usr/bin/cheetah-analyze:#!/usr/bin/python2
/usr/bin/cheetah-compile:#!/usr/bin/python2
/usr/bin/cloud-init:#!/usr/bin/python
/usr/bin/debuginfo-install:#!/usr/bin/python 
/usr/bin/easy_install:#!/usr/bin/python2
/usr/bin/easy_install-2.7:#!/usr/bin/python2
/usr/bin/find-repos-of-install:#!/usr/bin/python -tt
/usr/bin/firewall-cmd:#!/usr/bin/python -Es
/usr/bin/firewall-offline-cmd:#!/usr/bin/python -Es
/usr/bin/jsondiff:#!/usr/bin/python2
/usr/bin/jsonpatch:#!/usr/bin/python2
/usr/bin/jsonpointer:#!/usr/bin/python2
/usr/bin/markdown_py:#!/usr/bin/python2
/usr/bin/msghack:#!/usr/bin/python
/usr/bin/needs-restarting:#!/usr/bin/python -tt
/usr/bin/package-cleanup:#!/usr/bin/python
/usr/bin/pchrt:#! /usr/bin/python
/usr/bin/pflags:#! /usr/bin/python
/usr/bin/pip:#!/usr/bin/python2
/usr/bin/pip2:#!/usr/bin/python2
/usr/bin/pip2.7:#!/usr/bin/python2
/usr/bin/ptaskset:#! /usr/bin/python
/usr/bin/pydoc:#!/usr/bin/python2.7
/usr/bin/python2.7-config:#!/usr/bin/python2.7
/usr/bin/python2-config:#!/usr/bin/python2.7
/usr/bin/python-config:#!/usr/bin/python2.7
/usr/bin/repoclosure:#!/usr/bin/python -t
/usr/bin/repodiff:#!/usr/bin/python -tt
/usr/bin/repo-graph:#!/usr/bin/python
/usr/bin/repomanage:#!/usr/bin/python
/usr/bin/repoquery:#!/usr/bin/python -tt
/usr/bin/repo-rss:#!/usr/bin/python
/usr/bin/reposync:#!/usr/bin/python -tt
/usr/bin/repotrack:#!/usr/bin/python -tt
/usr/bin/show-changed-rco:#! /usr/bin/python -tt
/usr/bin/show-changed-rco:#!/usr/bin/python -tt
/usr/bin/show-installed:#!/usr/bin/python
/usr/bin/systemd-sysv-convert:#!/usr/bin/python
/usr/bin/urlgrabber:#!/usr/bin/python -t
/usr/bin/verifytree:#!/usr/bin/python -tt
/usr/bin/yum-builddep:#!/usr/bin/python -tt
/usr/bin/yum-config-manager:#!/usr/bin/python -tt
/usr/bin/yum-debug-dump:#!/usr/bin/python -tt
/usr/bin/yum-debug-restore:#!/usr/bin/python -tt
/usr/bin/yumdownloader:#!/usr/bin/python 
/usr/bin/yum-groups-manager:#!/usr/bin/python -tt

终于完成了!

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页