Centos系統(tǒng)目前默認(rèn)python環(huán)境版本號(hào)為2.7.5,在實(shí)際的開發(fā)宴偿、測(cè)試環(huán)境中我們可能需要2.7.13或者3.x的版本湘捎,但是Centos操作系統(tǒng)自帶軟件可能依賴python2.7.5版本,故原版本不能刪除窄刘,我們只能python基礎(chǔ)版本與高版本并存窥妇,以下為我實(shí)際的操作過程,有什么問題請(qǐng)指正????
第一步:查看Centos版本及python版本
-
Centos版本
[root@amio ~]# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
-
python版本
[root@amio ~]# python -V
Python 2.7.5
[root@amio usr]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
第二步:從官網(wǎng)下載python對(duì)應(yīng)版本的包(以2.7.13版本為例)
[root@amio ~]# cd /home/ # 存放下載包的路徑
[root@amio home]# wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz # wget 后接python官網(wǎng)對(duì)應(yīng)的鏈接
--2017-04-02 03:14:53-- https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
正在解析主機(jī) www.python.org (www.python.org)... xxx.xxx.xxx.xxx, 2a04:4e42:11::223
正在連接 www.python.org (www.python.org)|xxx.xxx.xxx.xxx|:443... 已連接娩践。
已發(fā)出 HTTP 請(qǐng)求活翩,正在等待回應(yīng)... 200 OK
長(zhǎng)度:17076672 (16M) [application/octet-stream]
正在保存至: “Python-2.7.13.tgz”
100%[==================================================================================>] 17,076,672 84.8MB/s 用時(shí) 0.2s
2017-04-02 03:14:53 (84.8 MB/s) - 已保存 “Python-2.7.13.tgz” [17076672/17076672])
[root@amio home]# ll
總用量 16680
-rw-r--r-- 1 root root 17076672 12月 17 20:21 Python-2.7.13.tgz
第三步:解壓、配置翻伺、編譯材泄、安裝python2.7.13
-
解壓
[root@amio home]# tar -zxvf Python-2.7.13.tgz # 解壓命令
[root@amio home]# ll # 解壓后在當(dāng)前目錄生成Python-2.7.13的目錄
總用量 16684
drwxr-xr-x 17 1000 1000 4096 12月 17 20:05 Python-2.7.13
-rw-r--r-- 1 root root 17076672 12月 17 20:21 Python-2.7.13.tgz
-
安裝gcc(在編譯時(shí)會(huì)依賴)
[root@amio home]# yum install gcc* openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel python-devel zlib
-
配置、編譯吨岭、安裝
[root@amio home]# cd Python-2.7.13
[root@amio Python-2.7.13]# (sudo) ./configure --prefix=/usr/local # [配置]指定可執(zhí)行文件拉宗、庫(kù)文件、配置文件辣辫、資源文件的安裝路徑旦事。若沒有權(quán)限加sudo
[root@amio Python-2.7.13]# (sudo) make # 編譯
[root@amio Python-2.7.13]# make altinstall # 不要使用make install,否則會(huì)覆蓋系統(tǒng)自帶python
第四步:安裝后環(huán)境檢查
-
python安裝后的版本
[root@amio ~]# python -V # 發(fā)現(xiàn)版本還是原版本
Python 2.7.5
-
安裝前后的python對(duì)比
[root@amio ~]# ll -l /usr/bin/python* # 系統(tǒng)自帶的
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
[root@amio ~]# ll -l /usr/local/bin/python* # 手工安裝的
-rwxr-xr-x 1 root root 8257136 4月 2 04:48 /usr/local/bin/python2.7
-rwxr-xr-x 1 root root 1687 4月 2 04:49 /usr/local/bin/python2.7-config
-
備份舊版本急灭,連接新版本
[root@amio ~]# mv /usr/bin/python /usr/bin/python2.7.5
[root@amio ~]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python2.7.5 -> python2 # 改為2.7.5
-rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
[root@amio ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python # 增加連接
[root@amio ~]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 24 4月 2 05:08 /usr/bin/python -> /usr/local/bin/python2.7 # 新增的姐浮,并指向新安裝的python
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python2.7.5 -> python2
-rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
-
再次檢查python版本
[root@amio ~]# python #正常展示python2.7.13版本
Python 2.7.13 (default, Apr 2 2017, 04:48:29)
[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版本)
[root@amio ~]# python2.7.5
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.
-
題外話:python2, python2.7訪問的是2.7.5還是2.7.13呢
[root@amio ~]# python2.7 #python2.7.13
Python 2.7.13 (default, Apr 2 2017, 04:48:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
[root@amio ~]# python2 #python2.7.5
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.
番外:yum的設(shè)置(系統(tǒng)預(yù)裝的yum引用的老版本python)
[root@amio ~]# yum -y install epel-release
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 # ??????這時(shí)候報(bào)錯(cuò)了
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.13 (default, Apr 2 2017, 04:48:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
[root@amio ~]# vi /usr/bin/yum
首行的#!/usr/bin/python 改為 #!/usr/bin/python2.7
改完之后繼續(xù)安裝,又報(bào)錯(cuò)??
ImportError: No module named urlgrabber.grabber
[root@amio ~]# vi /usr/libexec/urlgrabber-ext-down
首行的#!/usr/bin/python 改為 #!/usr/bin/python2.7