CentOS7.3编译安装python3.6

 一、前言

 最近迷上了python,所以准备开始上手python。由于python2维护较少,python3必然是趋势了,所以我准备用python3.6学习python。CentOS7.3默认安装的是python2.7,所以我需要从官网上下载python3.6并编译安装python3.6,但是比较尴尬的是,CentOS的yum环境支持python2而不支持python3,所以需要通过一些小小的修改,才能在正常的使用python3.6的同时也能正常的使用yum。这其实没什么难的,python3就按照正常的编译安装即可,yum环境支持只需要在/usr/bin/yum的以及/usr/libexec/urlgrabber-ext-down的第一行做一些软链接的修改即可。详细内容请看操作步骤。

 二、操作步骤

    1.编译环境准备

 yum groupinstall 'Development Tools' yum install zlib-devel bzip2-devel openssl-devel ncurese-devel

    2.下载python3.6源码包

wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2rc1.tar.xz

    3.解压压缩包

tar -xvf Python-3.6.2rc1.tar.xz

     4.编译安装步骤

    cd Python-3.6.2rc1/    ./configure --prefix=/usr/local/python3    make && make install

     5.修改软链接以及修改yum支持环境路径

     5.1修改软链接以及相关操作    mv /usr/bin/python2.7 /usr/bin/python2.7.5          #先更改python2.7的名字    rm /usr/bin/python -r                     #删除原先的软链接    ln -s /usr/bin/python2.7.5 /usr/bin/python2.7        #将python2.7环境软链接至/usr/bin/python2.7     ln -s /usr/local/python3/bin/python3.6 /usr/bin/python    #将python3.6环境软链接至/usr/bin/python    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip        #将pip3软链接至/usr/bin/pip    python -V                                                     #查看目前使用的python版本        Python 3.6.2rc1    pip -V                                                        #查看pip版本        pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)    python2.7 -V        Python 2.7.5                                               #当然你也可以使用python2.7.5    yum repolist                                                  #测试yum环境,yum环境失效    [root@localhost bin]# yum repolist    Loaded plugins: fastestmirror, langpacks    Loading mirror speeds from cached hostfile      File "/usr/libexec/urlgrabber-ext-down", line 28     #报错位置        except OSError, e:                      ^    SyntaxError: invalid syntax      File "/usr/libexec/urlgrabber-ext-down", line 28        except OSError, e:                      ^    SyntaxError: invalid syntax    Exiting on user cancel        5.2恢复yum环境相关操作    vi /usr/bin/yum    第一行修改为    #!/usr/bin/python2.7    vi /usr/libexec/urlgrabber-ext-down    第一行修改为    #!/usr/bin/python2.7        5.3测试yum环境    yum repolist                                                    #再次测试yum环境,yum环境正常            Loaded plugins: fastestmirror, langpacks        Loading mirror speeds from cached hostfile        repo id          repo name                                    status        cd               cd                                            9,363        centos           centos repository                             9,363        epel             extra packages for enterprise linux          11,349        repolist: 30,075