1. 程式人生 > >Centos7 升級python3,解決升級後不相容問題

Centos7 升級python3,解決升級後不相容問題

一、確實當前python版本

[[email protected] Python-3.6.1]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

以上輸出Python 2.7.5表示當前版本,因為yum使用的是Python2.7因此不能刪除,所以只能重新安裝一個替換版本。
輸入以下命令檢視目前系統使用的Python以及安裝目錄,接下來將要對該目錄下的Python進行修改

[[email protected] ~]# which python
/usr/bin/python
[[email protected] ~]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root    7 May 30 05:52 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 May 30 05:52 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov  6  2016 /usr/bin/python2.7

二、下載安裝

  1. 解壓Python安裝包
    tar -zxvf Python-3.6.1.tgz
  2. 解壓後進到Python根目錄檢視README檔案,裡面介紹了安裝方法,這裡跳過test步驟

Paste_Image.png


根據文件介紹執行第一步./configure –prefix=/usr/local/python3,後面新增引數為Python安裝目錄,預設安裝目錄為/usr/local/,如果出現以下錯誤提示則表示系統沒有安裝gcc
configure: error: no acceptable C compiler found in $PATH
安裝gcc重新執行./configure –prefix=/usr/local/python3即可

[[email protected] Python-3.6.1]# yum install gcc
[
[email protected]
Python-3.6.1]# ./configure –prefix=/usr/local/python3

./configure –prefix=/usr/local/python3執行成功後繼續下面的命令,跳過test
make #編譯
make install #安裝
安裝成功後在/usr/local/python3/bin目錄下執行python3.6,出現如下輸出證明Python已經安裝成功可以用了

[[email protected] python3]# python3.6 
Python 3.6.1 (default, Jun  3 2017, 07:13:03) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

三、替換舊Python

第一步已經確認當前使用Python為2.7.5並且在/usr/bin目錄下,接下來需要做的就是替換/usr/bin/python,並更改yum使用的直譯器,不然yum將無法使用
1 重新命名當前Python,結果如下


Paste_Image.png

2 新建Python3.6的軟連線到/usr/bin/python,建好後檢視已經存在,並指向正確

[[email protected] bin]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
[[email protected] bin]# ll /usr/bin/python                                    
lrwxrwxrwx. 1 root root 32 Jun  4 23:29 /usr/bin/python -> /usr/local/python3/bin/python3.6

3 直接輸入Python檢視版本,此時已是3.6.1


Paste_Image.png

四、解決升級Python後yum,Firewall等不能用的問題

1、 升級Python後執行yum會報錯


Paste_Image.png


vi /user/bin/yum 編輯yum的執行檔案,更改首行的/usr/bin/python 為上面重新命名的名字或/usr/bin/目錄下指向Python2.7的名字,之後再執行yum就正常了


Paste_Image.png
Paste_Image.png

2、 執行yum安裝其他軟體可能出現以下錯誤,這也是因為Python版本問題,
vi /usr/libexec/urlgrabber-ext-down 修改第一行的Python為重新命名的名字(指向python2.7的)

File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^

3、防火牆無法啟動


Paste_Image.png


出現以上情況根據提示修改/usr/bin/firewall-cmd檔案,順便將同目錄下的firewall-offline-cmd檔案進行修改,修改後執行啟動防火牆命令,會發現依然無法使用

[[email protected] ~]# systemctl start firewalld.service
Job for firewalld.service failed because the control process exited with error code. See "systemctl status firewalld.service" and "journalctl -xe" for details.

根據提示執行systemctl status firewalld.service命令檢視狀態,根據顯示中紅色部分可以看到,在執行/usr/sbin/firewalld檔案時出現問題,因為檢視改檔案


Paste_Image.png


可以看到這個檔案也是用Python編譯的,所以也要修改
vi /usr/sbin/firewalld

#!/usr/bin/python -Es
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010-2016 Red Hat, Inc.
# Authors:
# Thomas Woerner <[email protected]>

修改完成後再次執行啟動命令就可以正常啟動了,神清氣爽啊,哈哈哈

[[email protected] ~]# systemctl start firewalld.service 
[[email protected] ~]# systemctl status firewalld.service
â—  firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2017-06-07 05:38:13 CST; 6s ago
     Docs: man:firewalld(1)
 Main PID: 2481 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─2481 /usr/bin/python2.7 -Es /usr/sbin/firewalld --nofork --nopid

Jun 07 05:38:13 centos systemd[1]: Starting firewalld - dynamic firewall daemon...
Jun 07 05:38:13 centos systemd[1]: Started firewalld - dynamic firewall daemon.

除此之外還有一些其他檔案使用的Python,可以使用grep命令檢視哪些檔案使用了Python直譯器,根據具體情況修改。例如檢視/usr/bin目錄下的檔案

[[email protected] ~]# grep -ai /usr/bin/python /usr/bin/*
/usr/bin/easy_install:#!/usr/bin/python
/usr/bin/easy_install-2.7:#!/usr/bin/python
/usr/bin/firewall-cmd:#!/usr/bin/python2.7 -Es
/usr/bin/firewall-offline-cmd:#!/usr/bin/python2.7 -Es
/usr/bin/msghack:#!/usr/bin/python2.7
/usr/bin/pip:#!/usr/bin/python
/usr/bin/pip2:#!/usr/bin/python
/usr/bin/pip2.7:#!/usr/bin/python
/usr/bin/pydoc:#!/usr/bin/python2.7
/usr/bin/systemd-sysv-convert:#!/usr/bin/python2.7
/usr/bin/urlgrabber:#!/usr/bin/python2.7 -t
/usr/bin/wheel:#!/usr/bin/python2.7
/usr/bin/yum:#!/usr/bin/python2

五、make install過程中出現報錯解決

出現以下問題:
FAIL: test_with_pip (test.test_venv.EnsurePipTest)
zipimport.ZipImportError: can't decompress data; zlib not available


Paste_Image.png
Paste_Image.png

解決辦法:出現這種錯誤一般因為Python的安裝環境沒有安裝好,安裝以下軟體
yum install openssl-devel
如果還是有報錯則安裝Development tools
yum groupinstall Development tools
安裝成功後make clean以下,再次重複 上面操作