CentOS 7.3 安裝 Python2.7.9

由於需要安裝odoo 10.0,它所依賴的Python版本爲2.7.9,但是CentOS 7.3系統自帶的Python爲2.7.5。因爲有些关键应用程序依赖于Python 2.7.5,所以不能破壞系統的環境。如果替換啦系統的Python環境會發生很多難以預見的錯誤,甚至要重裝系統。
  安裝時使用make altinstall,如果使用make install,系統中將會有兩個不同版本的Python在/usr/bin目錄中,這將會導致很多問題,且不好處理。

若在安裝Python2.7.9之後,需要安裝pip,請在編譯之前檢查當前系統中的zlibzlib-developensslopenssl-devel軟件包是否正確安裝。否則,需要重新編譯安裝python2.7.9。可參考CentOS 7.3安裝pip問題記錄

當前相關版本
[hanzo@hanzo ~]$ cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[hanzo@hanzo ~]$ python --version
Python 2.7.5
#yum依賴python
[hanzo@hanzo ~]$ cat /usr/bin/yum
#!/usr/bin/python
import sys
......
下載Python2.7.9並解壓
[hanzo@hanzo Downloads]$ tar xfz Python-2.7.9.tgz 
[hanzo@hanzo Downloads]$ ls
Python-2.7.9.tgz  Python-2.7.9
安裝
[hanzo@hanzo Downloads]$ cd Python-2.7.9/
#--prefix,指定路徑以方便以後卸載(手動刪除)
[hanzo@hanzo Python-2.7.9]$ sudo ./configure --prefix=/usr/local/toolbox/python2.7.9
.....
[hanzo@hanzo Python-2.7.9]$ sudo make
......
[hanzo@hanzo Python-2.7.9]$ sudo make altinstall
......
[hanzo@hanzo Python-2.7.9]$ cd /usr/local/toolbox/python2.7.9/bin/
#創建軟鏈接,使用pythonu代替新版本中的python2.7程序,以區別舊版本的python
[hanzo@hanzo bin]$ sudo ln -s python2.7 ../bin/pythonu
配置環境變量

/etc/profile文件中追加以下內容:

export PATH=$PATH:/usr/local/toolbox/python2.7.9/bin
#更新當前終端環境變量
[hanzo@hanzo Python-2.7.9]$ source /etc/profile
#用tab進行提示
[hanzo@hanzo Python-2.7.9]$ python
python            python2           python2.7         python2.7-config  pythonu
[root@hanzo bin]# python --version
Python 2.7.5
[root@hanzo bin]# pythonu --version
Python 2.7.9
[root@hanzo bin]# pythonu
Python 2.7.9 (default, Nov 28 2017, 09:42:47) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

創建軟鏈接之後,使用python時,舊版本命令不變,新版本爲pythonu

新版本python使用‘python
[root@hanzo hanzo]# ll $(find /usr/bin/ -name python*) && echo '**********************' && ll $(find /usr/local/toolbox/python2.7.9/bin/ -name python*)
lrwxrwxrwx. 1 root root    7 Jul 27  2017 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 Jul 27  2017 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov  6  2016 /usr/bin/python2.7
**********************
-rwxr-xr-x. 1 root root 8209600 Nov 28 14:36 /usr/local/toolbox/python2.7.9/bin/python2.7
-rwxr-xr-x. 1 root root    1707 Nov 28 14:36 /usr/local/toolbox/python2.7.9/bin/python2.7-config
lrwxrwxrwx. 1 root root       9 Nov 28 11:31 /usr/local/toolbox/python2.7.9/bin/pythonu -> python2.7
#將原版本的python修改備份
[root@hanzo hanzo]# mv /usr/bin/python /usr/bin/python2.7.5
#爲了方便,直接修改上文的pythonu。
[root@hanzo hanzo]# mv /usr/local/toolbox/python2.7.9/bin/pythonu /usr/local/toolbox/python2.7.9/bin/python
[root@hanzo hanzo]# ll $(find /usr/bin/ -name python*) && echo '**********************' && ll $(find /usr/local/toolbox/python2.7.9/bin/ -name python*)
lrwxrwxrwx. 1 root root    9 Jul 27  2017 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov  6  2016 /usr/bin/python2.7
lrwxrwxrwx. 1 root root    7 Jul 27  2017 /usr/bin/python2.7.5 -> python2
**********************
lrwxrwxrwx. 1 root root       9 Nov 28 11:31 /usr/local/toolbox/python2.7.9/bin/python -> python2.7
-rwxr-xr-x. 1 root root 8209600 Nov 28 14:36 /usr/local/toolbox/python2.7.9/bin/python2.7
-rwxr-xr-x. 1 root root    1707 Nov 28 14:36 /usr/local/toolbox/python2.7.9/bin/python2.7-config
  • 也可直接創建軟鏈接,然後刪除pythonu(若有)
    ln -s /usr/local/toolbox/python2.7.9/bin/python2.7 /usr/local/toolbox/python2.7.9/bin/python
  • 對於新舊版本中都有python2.7,因環境變量配置export PATH=$PATH:$PYTHON_HOME/bin,原來的在前,所以python2.7依然對應原來的版本。

yum設置

[root@hanzo hanzo]# yum list
bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

修改/usr/bin/yum文件第一行/usr/bin/python/usr/bin/python2/usr/bin/python2.7/usr/bin/python2.7.5
對於/usr/libexec/urlgrabber-ext-down文件,第一行爲#! /usr/bin/python,也一起修改下。

你可能感兴趣的:(CentOS 7.3 安裝 Python2.7.9)