Centos 升级python2到python3版本

最近买了一个服务器,就是腾讯三年活动那个,里面内置的是2.7.5的版本,就想升级一下版本。
以下,是我一路踩坑所得。

下载

wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
[root@VM_135_58_centos ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
--2018-03-22 10:44:15--  https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
正在解析主机 www.python.org (www.python.org)... 151.101.40.223, 2a04:4e42:36::223
正在连接 www.python.org (www.python.org)|151.101.40.223|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:22710891 (22M) [application/octet-stream]
正在保存至: “Python-3.6.4.tgz”

100%[====================================================================================================================================================>] 22,710,891  2.40MB/s 用时 9.5s   

2018-03-22 10:44:30 (2.28 MB/s) - 已保存 “Python-3.6.4.tgz” [22710891/22710891])

[root@VM_135_58_centos ~]# 

解压

tar -xf Python-3.6.4.tgz
cd Python-3.6.4/
[root@VM_135_58_centos ~]# tar -xf Python-3.6.4.tgz 
[root@VM_135_58_centos ~]# ls
Python-3.6.4  Python-3.6.4.tgz
[root@VM_135_58_centos ~]# cd Python-3.6.4/
[root@VM_135_58_centos Python-3.6.4]# ls
aclocal.m4    config.sub  configure.ac  Grammar  install-sh  LICENSE  Makefile.pre.in  Modules  Parser  PCbuild   pyconfig.h.in  README.rst  Tools
config.guess  configure   Doc           Include  Lib         Mac      Misc             Objects  PC      Programs  Python         setup.py

安装

yum -y install sqlite-devel

先安装上面这个,不然在使用sqlite3的时候有一个No module named _sqlite3的错误

./configure --with-ssl
make & make install

记住一定要带–with-ssl,不然以后安装模块的时候可能会出现如下错误:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

配置的时候报错了:

[root@VM_135_58_centos Python-3.6.4]# ./configure 
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 `/root/Python-3.6.4':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
[root@VM_135_58_centos Python-3.6.4]# 

安装Gcc

yum -y install gcc

make install 的时候也报了一个错误:

can't decompress data; zlib not available

解决如下:

yum -y install zlib*

然后在执行安装命令

默认python3

到现在python3已经安装好了,可以用python3来看看时候有交互界面,但是python默认指向的还是python2,所以要修改软连接。

mv /usr/bin/python /usr/bin/python2.7.5
ln -s /usr/local/bin/python3.6 /usr/bin/python
[root@VM_135_58_centos bin]# mv /usr/bin/python /usr/bin/python2.7.5
[root@VM_135_58_centos bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python
[root@VM_135_58_centos bin]# python
Python 3.6.4 (default, Mar 22 2018, 10:52:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

yum 报错

执行yum命令的时候,会出现下面的错误

[root@VM_135_58_centos ~]# yum
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

修改yum命令中的python指向

vi /usr/bin/yum  

将第一行中的

#!/usr/bin/python

修改如下

#!/usr/bin/python2.7.5

yum 继续报错

 File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:

修改/usr/libexec/urlgrabber-ext-down文件头,和上面相同

yum即刻恢复正常

firewall 报错

vi /usr/bin/firewall-cmd
vi /usr/sbin/firewalld

将第一行中的

#!/usr/bin/python -Es 

修改如下

#!/usr/bin/python2.7.5 -Es

和yum解决方案一样

    原文作者:素笺淡墨染流年
    原文地址: https://www.jianshu.com/p/abf11aae5f9f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞