Centos7 安裝python3詳細教程,解決升級後不兼容問題

一、確實當前python版本

[root@centos Python-3.6.1]# pythonPython 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進行修改

[root@centos ~]# which python/usr/bin/python
[root@centos ~]# 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

二、下載安裝

官網下載需要的Python版本,通過 WinSCP(安裝使用教程)將下載的Python文件上傳到服務器目錄

  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即可

[root@centos Python-3.6.1]# yum install gcc[root@centos 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已經安裝成功可以用了

[root@centos 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,建好後查看已經存在,並指向正確

[root@centos bin]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python[root@centos 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文件進行修改,修改後執行啓動防火牆命令,會發現依然無法使用

[root@centos ~]# systemctl start firewalld.serviceJob 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](http://upload-images.jianshu.io/upload_images/6259206-24058c1d19fffa28.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
可以看到這個文件也是用Python編譯的,所以也要修改
`vi /usr/sbin/firewalld`

!/usr/bin/python -Es

-- coding: utf-8 --

Copyright (C) 2010-2016 Red Hat, Inc.

Authors:

Thomas Woerner [email protected]

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

[root@centos ~]# systemctl start firewalld.service
[root@centos ~]# 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目錄下的文件

[root@centos ~]# 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以下,再次重複 上面操作




轉載:https://www.jianshu.com/p/a538125371d4


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章