天天看點

【整理】CentOS5.6 更新 Python2.4.3 到 2.7.5

檢視 python 的版本 

<a href="http://my.oschina.net/moooofly/blog/142561#">?</a>

1

2

<code>[root@localhost ~]</code><code># python -v</code>

<code>python 2.4.3</code>

下載下傳并安裝 python-2.7.5 

3

4

5

6

<code>[root@localhost ~]</code><code># wget http://python.org/ftp/python/2.7.5/python-2.7.5.tar.bz2</code>

<code>[root@localhost ~]</code><code># tar -jxvf python-2.7.5.tar.bz2</code>

<code>[root@localhost ~]</code><code># cd python-2.7.5</code>

<code>[root@localhost python-2.7.5]</code><code># ./configure</code>

<code>[root@localhost python-2.7.5]</code><code># make</code>

<code>[root@localhost python-2.7.5]</code><code># make install</code>

建立軟連接配接,使系統預設的 python 指向 python2.7

      正常情況下,即使 python2.7 安裝成功後,系統預設指向的 python 仍然是 2.4.3 版本,考慮到 centos5.6 系統中的 yum 是基于 python2.4 才能正常工作,是以不要解除安裝 python2.4 版本。 那麼 如何 實作将系統預設的 python 指向到 2.7 版本呢? 

未做修改前: 

<code>[root@localhost python-2.7.5]</code><code># ll /usr/bin/python*</code>

<code>-rwxr-xr-x 2 root root 8304 mar  6  2011</code><code>/usr/bin/python</code>

<code>lrwxrwxrwx 1 root root    6 jul  4  2013</code><code>/usr/bin/python2</code> <code>-&gt; python</code>

<code>-rwxr-xr-x 2 root root 8304 mar  6  2011</code><code>/usr/bin/python2</code><code>.4</code>

做如下修改: 

<code>[root@localhost python-2.7.5]</code><code># rm -f /usr/bin/python2</code>

<code>[root@localhost python-2.7.5]</code><code># mv /usr/bin/python /usr/bin/python2.4</code>

<code>[root@localhost python-2.7.5]</code><code># ln -s /usr/local/bin/python2.7 /usr/bin/python</code>

上面的 3 步分别為: 

1.删除之前的軟連接配接; 

2.将預設 python 重命名為 python2.4 以給 yum 使用(這裡可以省略該步驟,因為 python2.4 本身就存在); 

3.将預設 python 軟連接配接到 python2.7 上(這裡要看新裝的 python2.7 是否已存在到 python 的軟連接配接)。 

檢驗 python 指向是否成功 

<code>[root@localhost python-2.7.5]</code><code># python -v</code>

<code>python 2.7.5</code>

解決預設 python 軟連結指向 python2.7 版本後 yum 不能正常工作的問題 

<code>[root@localhost python-2.7.5]</code><code># vi /usr/bin/yum</code>

<code>#!/usr/bin/python</code>

<code>...</code>

将檔案頭部的 

改成 

<code>#!/usr/bin/python2.4</code>

整個更新過程完成,可以使用 python2.7.5 版本了。 

============== 我是分割線   ============= 

當預設 python 更新到 2.7.5 後,在未修改 /usr/bin/yum 時,運作 yum 相關指令會得到如下錯誤: 

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<code>[root@localhost python-2.7.5]</code><code># yum list</code>

<code>there was a problem importing one of the python modules</code>

<code>required to run yum. the error leading to this problem was:</code>

<code>   </code><code>no module named yum</code>

<code>please</code><code>install</code> <code>a package</code><code>which</code> <code>provides this module, or</code>

<code>verify that the module is installed correctly.</code>

<code>it</code><code>'s possible that the above module doesn'</code><code>t match the</code>

<code>current version of python,</code><code>which</code> <code>is:</code>

<code>2.7.5 (default, jul  5 2013, 02:21:36)</code>

<code>[gcc 4.1.2 20080704 (red hat 4.1.2-54)]</code>

<code>if you cannot solve this problem yourself, please go to</code>

<code>the yum faq at:</code>

<code>  </code><code>http:</code><code>//wiki</code><code>.linux.duke.edu</code><code>/yumfaq</code>

<code>  </code> 

<code>[root@localhost python-2.7.5]</code><code>#</code>

這是因為 yum 對 python 版本具有依賴性的原因。/usr/bin/yum 的内容如下: 

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<code>import</code> <code>sys</code>

<code>try:</code>

<code>    </code><code>import</code> <code>yum</code>

<code>except importerror:</code>

<code>    </code><code>print &gt;&gt; sys.stderr,</code><code>""</code><code>"\</code>

<code>   </code><code>%s</code>

<code>%s</code>

<code>""</code><code>" % (sys.exc_value, sys.version)</code>

<code>    </code><code>sys.</code><code>exit</code><code>(1)</code>

<code>sys.path.insert(0,</code><code>'/usr/share/yum-cli'</code><code>)</code>

<code>    </code><code>import</code> <code>yummain</code>

<code>    </code><code>yummain.user_main(sys.argv[1:], exit_code=true)</code>

<code>except keyboardinterrupt, e:</code>

<code>    </code><code>print &gt;&gt; sys.stderr,</code><code>"\n\nexiting on user cancel."</code>

<code>~</code>

============== 我是分割線   2015-09-08 ============= 

上述變更方式,改動比較大,另外一種方式為,保留系統中已有 python 相關内容不變,僅通過變更環境變量的方式引用新版本 python 即可。

ps: 若出現錯誤 "importerror: no module named bz2" ,則需要先安裝 bzip2-devel  ,再重新編譯 python 即可(正統方法);或者,執行

<code>cp</code> <code>/usr/lib64/python2</code><code>.6</code><code>/lib-dynload/bz2</code><code>.so</code><code>/usr/local/lib/python2</code><code>.7/</code>