CentOS 7.2 安裝python3.6,相容原有的2.7

來源:互聯網
上載者:User

標籤:centos7 python3

很早以前就想著要寫部落格,因為做了很多事情,總是記不住,能寫下來,就當是日記了.


我這次想要在我的centos7裡面安裝python3.6,並且能與原來的2.7相容.

1、查看原來的python版本

[[email protected] ~]# python

Python 2.7.5 (default, Nov 20 2015, 02:00:19)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>>

原本的版本是2.7.5,我現在準備再安裝一個3.6的版本,而且安裝後腰注意一個問題,因為centos的yum工具使用的python是2.7,需要注意

2、下載安裝包,工欲善其事必先利其器,沒有安裝包,巧婦難為無米之炊嘛

python的安裝包可以到python的官網地址裡面去下載(https://www.python.org/ftp/python)

因為我的環境是windows上面的vm虛擬機器裡面的centos7,不想下載來本地再傳進去.所以直接使用wget下載到centos裡面去

(https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz)

[[email protected] ~]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

-bash: wget: 未找到命令

[[email protected] ~]# yum install wget

已載入外掛程式:fastestmirror

base                                                     | 3.6 kB     00:00    

......................

已安裝:

 wget.x86_64 0:1.14-15.el7_4.1                                                

完畢!

[[email protected] ~]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

............................................

正在串連 www.python.org (www.python.org)|151.101.72.223|:443... 已串連。

已發出 HTTP 要求,正在等待回應... 200 OK

長度:22673115 (22M) [application/octet-stream]

正在儲存至: “Python-3.6.3.tgz”

0% [                                       ] 183,323     2.05KB/s 剩餘 3h 17m

下載完成以後,解壓包裡面的檔案

[[email protected] ~]# ls

anaconda-ks.cfg  Python-3.6.3.tgz

[[email protected] ~]# tar -zxvf Python-3.6.3.tgz

Python-3.6.3/

Python-3.6.3/Doc/

................................

3、有了安裝的包以後,我們再來解決一個linux系統安裝軟體必須的一個步驟,那就是安裝一些必要的依賴包.

依賴這種東西,不好說,我只能是安裝可能需要到的東西

[[email protected] ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

...............

已安裝:

 bzip2-devel.x86_64 0:1.0.6-13.el7      expat-devel.x86_64 0:2.1.0-10.el7_3    

 gdbm-devel.x86_64 0:1.10-8.el7         openssl-devel.x86_64 1:1.0.2k-8.el7    

 readline-devel.x86_64 0:6.2-10.el7     sqlite-devel.x86_64 0:3.7.17-8.el7    

......................                            

完畢!

4、安裝了依賴包,就可以開始編譯安裝python了

[[email protected] Python-3.6.3]# ./configure --prefix=/usr/local/python3;make;make install

checking build system type... x86_64-pc-linux-gnu

checking host system type... x86_64-pc-linux-gnu

..........................

checking for cl.exe... no

configure: error: in `/root/Python-3.6.3‘:

configure: error: no acceptable C compiler found in $PATH

See `config.log‘ for more details

make: *** 沒有指明目標並且找不到 makefile。 停止。

make: *** 沒有規則可以建立目標“install”。 停止。

[[email protected] Python-3.6.3]#

這個錯誤需要安裝gcc來解決

[[email protected] Python-3.6.3]# yum  install  gcc

.....................................

已安裝:

 gcc.x86_64 0:4.8.5-16.el7                                                    

..................        

完畢!

安裝了gcc,就接著編譯安裝python

[[email protected] Python-3.6.3]# ./configure --prefix=/usr/local/python3;make;make install

......................

Collecting setuptools

Collecting pip

Installing collected packages: setuptools, pip

Successfully installed pip-9.0.1 setuptools-28.8.0

這就安裝完了.

5、修改原有python指向的2.7為3.5,並且修改yum配置

[[email protected] bin]# ll /usr/bin/py*

-rwxr-xr-x. 1 root root   78 11月 20 2015 /usr/bin/pydoc

lrwxrwxrwx. 1 root root    7 10月 30 19:00 /usr/bin/python -> python2

lrwxrwxrwx. 1 root root    9 10月 30 19:00 /usr/bin/python2 -> python2.7

-rwxr-xr-x. 1 root root 7136 11月 20 2015 /usr/bin/python2.7

這裡的/usr/bin/python -> python2是

原有的python使用的是/usr/bin/python2.7這個版本,我們這裡就要修改成3.6的版本

首先是把原來的備份,要不然出了問題修複起來麻煩,備份後就可以將python3軟串連到/usr/bin/目錄下

lrwxrwxrwx. 1 root root    7 10月 30 19:00 /usr/bin/python -> python2

lrwxrwxrwx. 1 root root    9 10月 30 19:00 /usr/bin/python2 -> python2.7

-rwxr-xr-x. 1 root root 7136 11月 20 2015 /usr/bin/python2.7

[[email protected] bin]# mv /usr/bin/python /usr/bin/python.bak

[[email protected] bin]# ll /usr/bin/py*

-rwxr-xr-x. 1 root root   78 11月 20 2015 /usr/bin/pydoc

lrwxrwxrwx. 1 root root    9 10月 30 19:00 /usr/bin/python2 -> python2.7

-rwxr-xr-x. 1 root root 7136 11月 20 2015 /usr/bin/python2.7

lrwxrwxrwx. 1 root root    7 10月 30 19:00 /usr/bin/python.bak -> python2

[[email protected] bin]# ln -s /usr/local/python3/bin/python3 /usr/bin/python

然後測試一下當前環境使用的python是什麼版本

[[email protected] bin]# python

Python 3.6.3 (default, Nov  9 2017, 00:02:58)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>

我們看到的是新裝的3.6版本了,接下來就去把yum的設定檔修改掉

[[email protected] bin]# vi /usr/bin/yum

#!/usr/bin/python

import sys

try:

   import yum

except ImportError:

   print >> sys.stderr, """\

There was a problem importing one of the Python modules

required to run yum. The error leading to this problem was:

  %s

上面的#!/usr/bin/python需要修改為#!/usr/bin/python2.7,修改後使用yum測試一下yum工具是否正常,修改後使用yum,發現有報錯:

Downloading packages:

 File "/usr/libexec/urlgrabber-ext-down", line 28

   except OSError, e:

                ^

SyntaxError: invalid syntax

由於使用者取消而退出

查看/usr/libexec/urlgrabber-ext-down檔案,發現裡面也有python參數,同樣的修改之

[[email protected] bin]# cat /usr/libexec/urlgrabber-ext-down

#! /usr/bin/python

#  A very simple external downloader

#  Copyright 2011-2012 Zdenek Pavlas

上面的#!/usr/bin/python需要修改為#!/usr/bin/python2.7,修改後使用yum再測試,正常了

[[email protected] bin]# yum install zip

.......................................

已安裝:

 zip.x86_64 0:3.0-11.el7                                        

完畢!

OK,查看系統裡面的python2和3共存

[[email protected] bin]# python

Python 3.6.3 (default, Nov  9 2017, 00:02:58)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> quit()

[[email protected] bin]# python2.7

Python 2.7.5 (default, Nov 20 2015, 02:00:19)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> quit()

OK,我的centos7安裝python3.6就算完成了


本文出自 “一路向上” 部落格,請務必保留此出處http://zhenghong.blog.51cto.com/1676992/1979991

CentOS 7.2 安裝python3.6,相容原有的2.7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.