准備環境:CentOS6.9機器
1.查看機器默認的Python版本
[root@hlmcent69nma ~]# python -V Python 2.6.6 [root@hlmcent69nma ~]# whereis python python: /usr/bin/python /usr/bin/python2.6 /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python /usr/include/python2.6 /usr/share/man/man1/python.1.gz
2.安裝gcc
[root@hlmcent69nma ~]# yum install gcc -y
3.下載最新的Python2.7.x安裝包,解壓並進入指定目錄
[root@hlmcent69nma ~]# wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz [root@hlmcent69nma ~]# ll Python-2.7.14.tgz -rw-r--r--. 1 root root 17176758 Sep 17 02:05 Python-2.7.14.tgz [root@hlmcent69nma ~]# tar zxvf Python-2.7.14.tgz [root@hlmcent69nma ~]# cd Python-2.7.14 [root@hlmcent69nma Python-2.7.14]# ll total 1000 -rw-r--r--. 1 1000 1000 10914 Sep 17 01:38 aclocal.m4 -rwxr-xr-x. 1 1000 1000 44259 Sep 17 01:38 config.guess -rwxr-xr-x. 1 1000 1000 36515 Sep 17 01:38 config.sub -rwxr-xr-x. 1 1000 1000 442959 Sep 17 01:38 configure -rw-r--r--. 1 1000 1000 141572 Sep 17 01:38 configure.ac drwxr-xr-x. 22 1000 1000 4096 Sep 17 01:38 Demo drwxr-xr-x. 18 1000 1000 4096 Sep 17 01:55 Doc drwxr-xr-x. 2 1000 1000 4096 Sep 17 01:38 Grammar drwxr-xr-x. 2 1000 1000 4096 Sep 17 01:38 Include -rwxr-xr-x. 1 1000 1000 7122 Sep 17 01:38 install-sh drwxr-xr-x. 47 1000 1000 12288 Sep 17 01:38 Lib -rw-r--r--. 1 1000 1000 12757 Sep 17 01:38 LICENSE drwxr-xr-x. 11 1000 1000 4096 Sep 17 01:38 Mac -rw-r--r--. 1 1000 1000 48553 Sep 17 01:38 Makefile.pre.in drwxr-xr-x. 5 1000 1000 4096 Sep 17 01:38 Misc drwxr-xr-x. 9 1000 1000 4096 Sep 17 01:38 Modules drwxr-xr-x. 3 1000 1000 4096 Sep 17 01:38 Objects drwxr-xr-x. 2 1000 1000 4096 Sep 17 01:38 Parser drwxr-xr-x. 9 1000 1000 4096 Sep 17 01:38 PC drwxr-xr-x. 2 1000 1000 4096 Sep 17 01:38 PCbuild -rw-r--r--. 1 1000 1000 35170 Sep 17 01:38 pyconfig.h.in drwxr-xr-x. 2 1000 1000 4096 Sep 17 01:38 Python -rw-r--r--. 1 1000 1000 55670 Sep 17 01:38 README drwxr-xr-x. 5 1000 1000 4096 Sep 17 01:38 RISCOS -rw-r--r--. 1 1000 1000 99231 Sep 17 01:38 setup.py drwxr-xr-x. 23 1000 1000 4096 Sep 17 01:38 Tools
4.配置,編譯,編譯安裝
[root@hlmcent69nma Python-2.7.14]# ./configure --prefix=/usr/local/python/python2.7 [root@hlmcent69nma Python-2.7.14]# make [root@hlmcent69nma Python-2.7.14]# make install
備注:
執行 ./configure --prefix=/usr/local/python/python3 命令。./configure命令執行完畢之后創建一個文件creating Makefile,供下面的make命令使用 執行make install之后就會把程序安裝到我們指定的目錄中去。Configure是一個可執行腳本,它有很多選項,在待安裝的源碼路徑下使用命令./configure –help輸出詳細的選項列表。其中--prefix選項是配置安裝的路徑,如果不配置該選項,安裝后可執行文件默認放在/usr /local/bin,庫文件默認放在/usr/local/lib,配置文件默認放在/usr/local/etc,其它的資源文件放在/usr /local/share,比較凌亂。如果配置--prefix,如:./configure --prefix=/usr/local/test可以把所有資源文件放在/usr/local/test的路徑中,不會雜亂。用了—prefix選項的另一個好處是卸載軟件或移植軟件。當某個安裝的軟件不再需要時,只須簡單的刪除該安裝目錄,就可以把軟件卸載得干干凈凈;移植軟件只需拷貝整個目錄到另外一個機器即可(相同的操作系統)。當然要卸載程序,也可以在原來的make目錄下用一次make uninstall,但前提是make文件指定過uninstall。
5.查看系統的Python版本,還是2.6.x
[root@hlmcent69nma ~]# python -V Python 2.6.6
6.查看新安裝的Python版本,當前系統的Python版本,並將系統指向的Python從2.6.x修改為2.7.x,再次查看當前系統的Python版本,已經變更為2.7.x
[root@hlmcent69nma ~]# /usr/local/python/python2.7/bin/python2.7 -V Python 2.7.14 [root@hlmcent69nma ~]# /usr/bin/python -V Python 2.6.6 [root@hlmcent69nma ~]# mv /usr/bin/python /usr/bin/python.bak [root@hlmcent69nma ~]# ln -s /usr/local/python/python2.7/bin/python2.7 /usr/bin/python [root@hlmcent69nma ~]# python -V Python 2.7.14
7.但是發現yum命令無法使用了,由於yum是基於python2.6才能正常工作,需單獨將yum指向python2.6版本
[root@hlmcent69nma ~]# yum list There was a problem importing one of the Python modules required to run yum. The error leading to this problem was: No module named yum Please install a package which provides this module, or verify that the module is installed correctly. It's possible that the above module doesn't match the current version of Python, which is: 2.7.14 (default, Nov 30 2017, 11:54:55) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] If you cannot solve this problem yourself, please go to the yum faq at: http://yum.baseurl.org/wiki/Faq
[root@hlmcent69nma ~]# vim /usr/bin/yum
參考鏈接:
https://www.cnblogs.com/perallina/p/5253329.html
https://www.cnblogs.com/lclq/archive/2016/06/27/5620196.html