====================================================================================================================================
日期:2015-09-04
環境:red hat 6-64bit
描述:之前只在windows下小小的玩過Python,大三狗終於下定決心要系統的學學Python了!
我的虛擬機裝的是red hat6.4-64bit,系統自帶的Python版本是2.6,爲了更方便的學習,所以決心安裝/升級一下
====================================================================================================================================
日期:2015-09-08
描述:升級完python隔天用了yum命令,竟然報錯了···查了資料才發現原來yum命令是python寫的==
看了一圈資料沒直接解決問題,不過受到啓發認真地回顧了一下安裝python的過程。故完善和修改一下安裝日誌!
報錯信息如下:
bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory
//原來/usr/bin/yum腳本的首行標註着此腳本執行的解釋器爲 #!/usr/bin/python,此時報錯提示沒有找到
//按網上的教程修改後依舊報錯:
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.3 (default, Sep 3 2015, 16:58:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
====================================================================================================================================一、關於Python的安裝包
用wget或直接在官網(http://www.python.org/ftp/python/)下載tar包即可[不過要現在要翻牆了==]。
最新的Python2版本目前出到2.7.10了。
二、安裝Python2.7.10
安裝方法就是典型的編譯安裝“三部曲”。
(1)解壓
[root@localhost soft]# tar xvf Python-2.7.10.tar.xz
[root@localhost soft]# ls Python2.7.10
aclocal.m4 Demo Mac PC README
build Doc Makefile PCbuild RISCOS
config.guess Grammar Makefile.pre pybuilddir.txt setup.py
config.log Include Makefile.pre.in pyconfig.h Tools
config.status install-sh Misc pyconfig.h.in
config.sub Lib Modules python
configure libpython2.7.a Objects Python
configure.ac LICENSE Parser python-gdb.py
(2)編譯
[root@localhost Python-2.7.10]# yum install gcc zlib zlib-devel(用的本地yum)
[root@localhost Python-2.7.10]# ./comfigure --prefix=/usr/local/python2.7.10 (--prefix是指定安裝路徑)
(3)安裝
[root@localhost Python-2.7.10]# make ; make insall
[root@localhost soft]# ls /usr/local/python2.7.10/ //查看安裝文件
bin include lib share
[root@localhost ~]# /usr/local/python2.7.10/bin/python2.7 //打開python2.7.10的方法
Python 2.7.10 (default, Sep 8 2015, 04:54:27)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
三、如何使得python命令默認指向新版本
即使是Python2.7.10安裝好了,但系統原來的python命令打開的還是原來的2.6版本。查看一下系統現在的python命令指向哪裏
[root@localhost ~]# which python
/usr/bin/python
[root@localhost ~]# whereis python
python: /usr/bin/python2.6 /usr/bin/python /usr/lib/python2.6 /usr/lib64/python2.6 /usr/include/python2.6 /usr/share/man/man1/python.1.gz
那麼如何改變呢?有如下3種方法:
[方法1] 添加別名並寫入.bashrc文件中
alias python='/usr/local/python2.7.10/bin/python2.7'
//實驗證明,這種方法因爲沒有改動到yum命令執行腳本所用到的/usr/bin/python,所以這種方法不會影響到yum命令的使用
//值得注意的是:alias後面加的路徑一定要到python2.7那個腳本爲止,這和後面的兩種方法是不一樣的
[方法2] 在/etc/profile文件中添加環境變量
[root@localhost ~]# vim /etc/profile
export PATH=$PATH:/usr/local/python2.7.10/bin //這裏寫的和方法1就不一樣了<pre name="code" class="python">
[root@localhost ~]# source /etc/profile //表示立即更新這個配置文件
[方法3] 在.bashrc文件中添加環境變量
[root@localhost ~]# vim .bashrc
export PATH=$PATH:/usr/local/python2.7.10/bin <pre name="code" class="python">
注意一下要重新打開終端才能使這個.bashrc文件生效哦
方法2或3添加環境變量後,還需要將原來的/usr/bin/python文件改動一下才能達到效果。即使用python命令時要讓系統找不到它,然後去PATH變量去找,才能達到我們的目的。這裏採取的方法是將/usr/bin/python重命名:
[root@localhost ~]# mv /usr/bin/python /usr/bin/python2.6.6 //這個怎麼處理都可以,但是不要影響改變到原來whereis查看到的那些路徑
仔細想想,原來yum命令執行腳本的首行是什麼呢?
[root@localhost ~]# cat /usr/bin/yum
#!/usr/bin/python
import sys
······
//而我們是不是將這個文件重命名了?
//所以爲了不影響yum命令的使用,將此腳本的第一行改成 #!/usr/bin/python2.6.6 即可(即我們重命名後的文件名)
至此,安裝/升級完成!且沒有影響到yum的使用!
[root@localhost /]# python
Python 2.7.10 (default, Sep 8 2015, 04:54:27)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>