1:检查当前Linux版本&Python版本信息
# more /etc/redhat-release
Red Hat Enterprise Linux Server release 6.6 (Santiago)
# python -V
Python 2.6.6
2:下载、解压Python安装包
注意官方网址提供各个版本,以及不同压缩格式的安装包,选择合适自己的即可。如果Linux不能访问外网,也可以手工下载上传。此处略过!
#wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
#tar -xzvf Python-3.6.6.tgz
3:编译安装Python 3.6.6
#cd Python-3.6.6
#./configure --prefix=/usr/local
# make
# make install 或者 make altinstall
# make clean
安装报错:
[root@test1 Python-3.6.6]# ./configure --prefix=/usr/local
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.6... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/home/python/Python-3.6.6':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
解决办法:
由于本机缺少gcc编译环境
通过yum安装gcc编译环境
yum install -y gcc
注释:
./configure 是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本
make 是用来编译的,它从Makefile中读取指令,然后编译。
make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
make clean:清除编译产生的可执行文件及目标文件(object file,*.o)。
注意make install 与 make altinstall的区别:
install 除了做 altinstall 外还会做 bininstall , maninstall 共三个动作bininstall 就是在处理 Symbolic Link Chain 的相关事务, 而 maninstall 则是在产生 unversioned manual pages, 所以, 很明显的, 不使用 bininstall 可以避免 python install 时 update link 的问题。如果使用make install,你将会看到在系统中有两个不同版本的Python在/usr/bin/目录中。这将会导致很多问题,而且不好处理。
4:验证是否升级成功
# python -V
Python 2.7
# /usr/local/bin/python3.6 -V
Python 3.6.6
5:设置环境变量
[root@master Python-3.6.6]# cd /usr/bin/
[root@master bin]# ll |grep python
lrwxrwxrwx 1 root root 7 Jun 19 14:49 python -> python2
lrwxrwxrwx 1 root root 9 Jun 19 14:49 python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Apr 9 22:31 python2.7
-rwxr-xr-x 1 root root 1835 Apr 9 22:31 python2.7-config
lrwxrwxrwx 1 root root 16 Jun 19 14:51 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 Jun 19 14:51 python-config -> python2-config
[root@master bin]# mv /usr/bin/python /usr/bin/python.bak
[root@master bin]# ll |grep python
lrwxrwxrwx 1 root root 9 Jun 19 14:49 python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Apr 9 22:31 python2.7
-rwxr-xr-x 1 root root 1835 Apr 9 22:31 python2.7-config
lrwxrwxrwx 1 root root 16 Jun 19 14:51 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 7 Jun 19 14:49 python.bak -> python2
lrwxrwxrwx 1 root root 14 Jun 19 14:51 python-config -> python2-config
[root@master bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python
[root@master bin]# ll |grep python
lrwxrwxrwx 1 root root 24 Aug 19 17:22 python -> /usr/local/bin/python3.6
lrwxrwxrwx 1 root root 9 Jun 19 14:49 python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Apr 9 22:31 python2.7
-rwxr-xr-x 1 root root 1835 Apr 9 22:31 python2.7-config
lrwxrwxrwx 1 root root 16 Jun 19 14:51 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 7 Jun 19 14:49 python.bak -> python2
lrwxrwxrwx 1 root root 14 Jun 19 14:51 python-config -> python2-config
[root@master bin]# python -V
Python 3.6.6
[root@master bin]# more /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@master bin]#
6:配置yum
如下所示,升级 Python 之后,由于将默认的python指向了python 3.6,yum不能正常使用,需要编辑 yum 的配置文件
验证下yum是否可用
[root@master bin]# yum search mysql
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
[root@master bin]#
编辑vim /usr/bin/yum 配置文件,将第一行记录#!/usr/bin/python改为#!/usr/bin/python2.7
[root@master bin]# vim /usr/bin/yum
#!/usr/bin/python2.7
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)
updates | 2.9 kB 00:00:00
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
解决办法:
vim /usr/libexec/urlgrabber-ext-down
指向之前的版本号
#!/usr/bin/python2.7