Centos7.4系统Python2.7升级Python3.6(编译安装)

Posted 抛物线.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7.4系统Python2.7升级Python3.6(编译安装)相关的知识,希望对你有一定的参考价值。

Centos7.4系统Python2.7升级Python3.6

1、下载安装包(编译安装)
# 下载安装包
[root@python3 ~]# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz

# 解压缩
[root@python3 ~]# xz -d Python-3.6.1.tar.xz
[root@python3 ~]# tar xvf Python-3.6.1.tar

2、编译安装
1. 新建python3目录
[root@python3 ~]#  mkdir /usr/local/python3
2. 进入源码文件目录
[root@python3 ~]# cd Python-3.6.1
3. 编译安装
[root@python3 ~]# ./configure --prefix=/usr/local/python3 --enable-optimizations 
[root@python3 ~]# make 
[root@python3 ~]# make install

3、替换
1.备份
[root@python3 ~]# cd /usr/bin  &&  mv python python.bak
2. 链接
[root@python3 ~]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
[root@python3 ~]# python -V
Python 3.6.1
[root@python3 ~]# python2 -V
Python 2.7.5
4、更新配置文件
1. 更改yum,把第一行  #!/usr/bin/python 替换成#!/usr/bin/python2

[root@python3 ~]# cat /usr/bin/yum
#!/usr/bin/python2
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

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:
%s

If you cannot solve this problem yourself, please go to
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

""" % (sys.exc_value, sys.version)
    sys.exit(1)

sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "\\n\\nExiting on user cancel."
    sys.exit(1)
[root@python3 ~]#


===============
2. 更改 /usr/libexec/urlgrabber-ext-down  把第一行  #!/usr/bin/python 替换成#!/usr/bin/python2

[root@python3 ~]# cat  /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python2
#  A very simple external downloader
#  Copyright 2011-2012 Zdenek Pavlas

#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the
#      Free Software Foundation, Inc.,
#      59 Temple Place, Suite 330,
#      Boston, MA  02111-1307  USA

import time, os, errno, sys
from urlgrabber.grabber import \\
    _readlines, URLGrabberOptions, _loads, \\
    PyCurlFileObject, URLGrabError

def write(fmt, *arg):
    try: os.write(1, fmt % arg)
    except OSError, e:
        if e.args[0] != errno.EPIPE: raise
        sys.exit(1)

class ProxyProgress:
    def start(self, *d1, **d2):
        self.next_update = 0
    def update(self, _amount_read):
        t = time.time()
        if t < self.next_update: return
        self.next_update = t + 0.31
        write('%d %d\\n', self._id, _amount_read)

def main():
    import signal
    signal.signal(signal.SIGINT, lambda n, f: sys.exit(1))
    cnt = 0
    while True:
        lines = _readlines(0)
        if not lines: break
        for line in lines:
            cnt += 1
            opts = URLGrabberOptions()
            opts._id = cnt
            for k in line.split(' '):
                k, v = k.split('=', 1)
                setattr(opts, k, _loads(v))
            if opts.progress_obj:
                opts.progress_obj = ProxyProgress()
                opts.progress_obj._id = cnt

            dlsz = dltm = 0
            try:
                fo = PyCurlFileObject(opts.url, opts.filename, opts)
                fo._do_grab()
                fo.fo.close()
                size = fo._amount_read
                if fo._tm_last:
                    dlsz = fo._tm_last[0] - fo._tm_first[0]
                    dltm = fo._tm_last[1] - fo._tm_first[1]
                ug_err = 'OK'
            except URLGrabError, e:
                size = 0
                ug_err = '%d %d %s' % (e.errno, getattr(e, 'code', 0), e.strerror)
            write('%d %d %d %.3f %s\\n', opts._id, size, dlsz, dltm, ug_err)

if __name__ == '__main__':
    main()
[root@python3 ~]#
5、 yum方式
# 当然根据场景方式不同,也可以直接yum

yum install make gcc gcc-c++  zlib*  python3-devel python3 -y  

以上是关于Centos7.4系统Python2.7升级Python3.6(编译安装)的主要内容,如果未能解决你的问题,请参考以下文章

centos6.5系统自带python2.6升级到python2.7

centos升级系统自带的python2.6为python2.7

python2.6 升级到 python2.7

python2.6.6升级python2.7.14

升级Python2.7

linux 升级python2.7 安装setuptools