centos7升级Python2.x到3.x,,CentOS 7中默


CentOS 7中默认安装了Python,版本比较低(2.7.5),为了使用新版3.x,需要对旧版本进行升级。由于很多基本的命令、软件包都依赖旧版本,比如:yum。所以,在更新Python时,建议不要删除旧版本(新旧版本可以共存)。

一、查看当前Python版本

# python -V

[[email protected] ~]# python -VPython 2.7.5

二、下载新的Python包并安装

进入python官网(https://www.python.org),选择需要的版本。此处选择Python-3.6.3版本

# yum install gcc gcc-c++ -y

Updated:  gcc.x86_64 0:4.8.5-36.el7_6.1                                               gcc-c++.x86_64 0:4.8.5-36.el7_6.1                                              Dependency Updated:  cpp.x86_64 0:4.8.5-36.el7_6.1                         gcc-gfortran.x86_64 0:4.8.5-36.el7_6.1           libgcc.x86_64 0:4.8.5-36.el7_6.1                     libgfortran.x86_64 0:4.8.5-36.el7_6.1                 libgomp.x86_64 0:4.8.5-36.el7_6.1                libquadmath.x86_64 0:4.8.5-36.el7_6.1                libquadmath-devel.x86_64 0:4.8.5-36.el7_6.1           libstdc++.x86_64 0:4.8.5-36.el7_6.1              libstdc++-devel.x86_64 0:4.8.5-36.el7_6.1          Complete!

下载

# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz

[[email protected] ~]# wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz--2019-04-11 23:45:29--  https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgzResolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:1a::223Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 22673115 (22M) [application/octet-stream]Saving to: ‘Python-3.6.3.tgz’100%[===================================================================================================================>] 22,673,115  73.7KB/s   in 3m 23s 2019-04-11 23:48:53 (109 KB/s) - ‘Python-3.6.3.tgz’ saved [22673115/22673115][[email protected] ~]# [[email protected] ~]# lsanaconda-ks.cfg  Python-3.6.3.tgz[[email protected] ~]# [[email protected] ~]# du -sh Python-3.6.3.tgz 22M     Python-3.6.3.tgz

解压

# tar zxvf Python-3.6.3.tgz

[[email protected] ~]# lsanaconda-ks.cfg  Python-3.6.3  Python-3.6.3.tgz  [[email protected] ~]# 

查看文件内容

[[email protected] ~]# cd Python-3.6.3[[email protected] Python-3.6.3]# [[email protected] Python-3.6.3]# lsaclocal.m4    config.sub  configure.ac  Grammar  install-sh  LICENSE  Makefile.pre.in  Modules  Parser  PCbuild   pyconfig.h.in  README.rst  Toolsconfig.guess  configure   Doc           Include  Lib         Mac      Misc             Objects  PC      Programs  Python         setup.py[[email protected] Python-3.6.3]# [[email protected] Python-3.6.3]# lltotal 1036-rw-r--r--.  1 501 501  13335 Oct  3  2017 aclocal.m4-rwxr-xr-x.  1 501 501  44259 Oct  3  2017 config.guess-rwxr-xr-x.  1 501 501  36515 Oct  3  2017 config.sub-rwxr-xr-x.  1 501 501 484734 Oct  3  2017 configure-rw-r--r--.  1 501 501 160430 Oct  3  2017 configure.acdrwxr-xr-x. 18 501 501   4096 Oct  3  2017 Docdrwxr-xr-x.  2 501 501     20 Oct  3  2017 Grammardrwxr-xr-x.  2 501 501   4096 Oct  3  2017 Include-rwxr-xr-x.  1 501 501   7122 Oct  3  2017 install-shdrwxr-xr-x. 33 501 501   8192 Oct  3  2017 Lib-rw-r--r--.  1 501 501  12757 Oct  3  2017 LICENSEdrwxr-xr-x.  8 501 501   4096 Oct  3  2017 Mac-rw-r--r--.  1 501 501  59746 Oct  3  2017 Makefile.pre.indrwxr-xr-x.  2 501 501   4096 Oct  3  2017 Miscdrwxr-xr-x. 13 501 501   4096 Oct  3  2017 Modulesdrwxr-xr-x.  4 501 501   4096 Oct  3  2017 Objectsdrwxr-xr-x.  2 501 501   4096 Oct  3  2017 Parserdrwxr-xr-x.  6 501 501   4096 Oct  3  2017 PCdrwxr-xr-x.  2 501 501   4096 Oct  3  2017 PCbuilddrwxr-xr-x.  2 501 501     79 Oct  3  2017 Programs-rw-r--r--.  1 501 501  41453 Oct  3  2017 pyconfig.h.indrwxr-xr-x.  3 501 501   4096 Oct  3  2017 Python-rw-r--r--.  1 501 501   9269 Oct  3  2017 README.rst-rw-r--r--.  1 501 501 101514 Oct  3  2017 setup.pydrwxr-xr-x. 23 501 501   4096 Oct  3  2017 Tools

# ./configure

[[email protected] Python-3.6.3]# ./configure …………………………………………creating Modules/Setupcreating Modules/Setup.localcreating MakefileIf you want a release build with all stable optimizations active (PGO, etc),please run ./configure --enable-optimizations

编译安装

# make && make install

报了一个错误

zipimport.ZipImportError: can‘t decompress data; zlib not availablemake: *** [install] Error 1

验证

[[email protected] ~]# python -VPython 2.7.5[[email protected] ~]# python3 -VPython 3.6.3

设置3.6.3为默认版本

查看Python 的路径,在 /usr/bin 下面。可以看到 python 链接的是 python 2.7,所以,执行 python 就相当于执行 python 2.7

# ll /usr/bin/ | grep python

[[email protected] ~]# ll /usr/bin/ | grep pythonlrwxrwxrwx.   1 root root          7 Mar 16 18:02 python -> python2lrwxrwxrwx.   1 root root          9 Mar 16 18:02 python2 -> python2.7-rwxr-xr-x.   1 root root       7136 Nov 20  2015 python2.7[[email protected] ~]# 

将原来python 的软链接重命名:

# mv /usr/bin/python /usr/bin/python.bak

将python 链接至 python3:

# ln -s /usr/local/bin/python3 /usr/bin/python

[[email protected] ~]# ll /usr/bin/ | grep pythonlrwxrwxrwx.   1 root root          7 Mar 16 18:02 python -> python2lrwxrwxrwx.   1 root root          9 Mar 16 18:02 python2 -> python2.7-rwxr-xr-x.   1 root root       7136 Nov 20  2015 python2.7[[email protected] ~]# [[email protected] ~]# mv /usr/bin/python /usr/bin/python.bak[[email protected] ~]# [[email protected] ~]# ll /usr/bin/ | grep pythonlrwxrwxrwx.   1 root root          9 Mar 16 18:02 python2 -> python2.7-rwxr-xr-x.   1 root root       7136 Nov 20  2015 python2.7lrwxrwxrwx.   1 root root          7 Mar 16 18:02 python.bak -> python2[[email protected] ~]# [[email protected] ~]# ln -s /usr/local/bin/python3 /usr/bin/python[[email protected] ~]# [[email protected] ~]# ll /usr/bin/ | grep pythonlrwxrwxrwx.   1 root root         22 Apr 12 00:24 python -> /usr/local/bin/python3lrwxrwxrwx.   1 root root          9 Mar 16 18:02 python2 -> python2.7-rwxr-xr-x.   1 root root       7136 Nov 20  2015 python2.7lrwxrwxrwx.   1 root root          7 Mar 16 18:02 python.bak -> python2[[email protected] ~]# 

配置YUM

升级Python 之后,由于将默认的 python 指向了 python3,yum 不能正常使用,需要编辑 yum 的配置文件,此时:

[[email protected] ~]# yum list  File "/usr/bin/yum", line 30    except KeyboardInterrupt, e:                            ^SyntaxError: invalid syntax

修改/usr/bin/yum和/usr/libexec/urlgrabber-ext-down,将#!/usr/bin/python 改为 #!/usr/bin/python2.7,保存退出即可。

# python -V

[[email protected] ~]# python -VPython 3.6.3

参考博客:

https://www.cnblogs.com/lwf-blog/p/8182975.html

https://www.cnblogs.com/fjping0606/p/9156344.html

https://www.cnblogs.com/simuhunluo/p/7704765.html

centos7升级Python2.x到3.x

评论关闭