1.查看python版本
[root@gxl-1 OP]# python --version
Python 2.7.5
2.下载安装包
[root@gxl-1 python]# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
3.解压缩
[root@gxl-1 python3]# xz -d Python-3.6.1.tar.xz
[root@gxl-1 python3]# tar xvf Python-3.6.1.tar
4.编译安装
#新建python3目录
[root@gxl-1 python3]# mkdir /usr/local/python3
#进入源码文件目录
[root@gxl-1 python3]# cd Python-3.6.1
#编译安装
[root@gxl-1 Python-3.6.1]# ./configure --prefix=/usr/local/python3/ --enable-optimizations
[root@gxl-1 Python-3.6.1]# make
[root@gxl-1 Python-3.6.1]# make install
5.替换
#备份
[root@gxl-1 Python-3.6.1]# cd /usr/bin/ && mv python.bak
#链接
[root@gxl-1 python3]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
#这里提示创建符号链接失败,但文件存在
#强制执行
[root@gxl-1 python3]# ln -sf /usr/local/python3/bin/python3.6 /usr/bin/python
#查看版本号
[root@gxl-1 python3]# python -V
Python 3.6.1
[root@gxl-1 python3]# python2 -V
Python 2.7.5
6.更改配置文件
#将yum第一行改为#!/usr/bin/python2
[root@gxl-1 python3]# vim /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)
#将/usr/libexec/urlgrabber-ext-down第一行改为#! /usr/bin/python2
[root@gxl-1 python3]# vim /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
7.yum方式
#根据场景方式不同,可以直接yum
yum install make gcc gcc-c++ zlib* python3-devel python3 -y