centos7升级python_centos7升级Python2.x到3.x

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

一、查看当前Python版本

# python -V

[root@localhost ~]# python -V

Python2.7.5

二、下载新的Python包并安装

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

# yum install gcc gcc-c++ -y

Updated:

gcc.x86_640:4.8.5-36.el7_6.1 gcc-c++.x86_64 0:4.8.5-36.el7_6.1Dependency Updated:

cpp.x86_640: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.1libgfortran.x86_640: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.1libquadmath-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.1Complete!

下载

# 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

[root@localhost ~]# 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.tgz

Resolving 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...200OK

Length:22673115 (22M) [application/octet-stream]

Saving to: ‘Python-3.6.3.tgz’100%[===================================================================================================================>] 22,673,115 73.7KB/s in3m 23s2019-04-11 23:48:53 (109 KB/s) - ‘Python-3.6.3.tgz’ saved [22673115/22673115]

[root@localhost~]#

[root@localhost~]# ls

anaconda-ks.cfg Python-3.6.3.tgz

[root@localhost~]#

[root@localhost~]# du -sh Python-3.6.3.tgz

22M Python-3.6.3.tgz

解压

# tar zxvf Python-3.6.3.tgz

[root@localhost ~]# ls

anaconda-ks.cfg Python-3.6.3 Python-3.6.3.tgz

[root@localhost~]#

查看文件内容

[root@localhost ~]# cd Python-3.6.3[root@localhost Python-3.6.3]#

[root@localhost Python-3.6.3]# ls

aclocal.m4 config.sub configure.ac Grammar install-sh LICENSE Makefile.pre.in Modules Parser PCbuild pyconfig.h.inREADME.rst Tools

config.guess configure Doc Include Lib Mac Misc Objects PC Programs Python setup.py

[root@localhost Python-3.6.3]#

[root@localhost Python-3.6.3]# ll

total1036

-rw-r--r--. 1 501 501 13335 Oct 3 2017aclocal.m4-rwxr-xr-x. 1 501 501 44259 Oct 3 2017config.guess-rwxr-xr-x. 1 501 501 36515 Oct 3 2017config.sub-rwxr-xr-x. 1 501 501 484734 Oct 3 2017configure-rw-r--r--. 1 501 501 160430 Oct 3 2017configure.ac

drwxr-xr-x. 18 501 501 4096 Oct 3 2017Doc

drwxr-xr-x. 2 501 501 20 Oct 3 2017Grammar

drwxr-xr-x. 2 501 501 4096 Oct 3 2017Include-rwxr-xr-x. 1 501 501 7122 Oct 3 2017 install-sh

drwxr-xr-x. 33 501 501 8192 Oct 3 2017Lib-rw-r--r--. 1 501 501 12757 Oct 3 2017LICENSE

drwxr-xr-x. 8 501 501 4096 Oct 3 2017Mac-rw-r--r--. 1 501 501 59746 Oct 3 2017 Makefile.pre.indrwxr-xr-x. 2 501 501 4096 Oct 3 2017Misc

drwxr-xr-x. 13 501 501 4096 Oct 3 2017Modules

drwxr-xr-x. 4 501 501 4096 Oct 3 2017Objects

drwxr-xr-x. 2 501 501 4096 Oct 3 2017Parser

drwxr-xr-x. 6 501 501 4096 Oct 3 2017PC

drwxr-xr-x. 2 501 501 4096 Oct 3 2017PCbuild

drwxr-xr-x. 2 501 501 79 Oct 3 2017Programs-rw-r--r--. 1 501 501 41453 Oct 3 2017 pyconfig.h.indrwxr-xr-x. 3 501 501 4096 Oct 3 2017Python-rw-r--r--. 1 501 501 9269 Oct 3 2017README.rst-rw-r--r--. 1 501 501 101514 Oct 3 2017setup.py

drwxr-xr-x. 23 501 501 4096 Oct 3 2017 Tools

# ./configure

[root@localhost Python-3.6.3]# ./configure

………………

………………

…………

creating Modules/Setup

creating Modules/Setup.local

creating Makefile

If 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 available

make: *** [install] Error 1

验证

[root@localhost ~]# python -V

Python2.7.5[root@localhost~]# python3 -V

Python3.6.3

设置3.6.3为默认版本

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

# ll /usr/bin/ | grep python

[root@localhost ~]# ll /usr/bin/ |grep python

lrwxrwxrwx.1 root root 7 Mar 16 18:02 python ->python2

lrwxrwxrwx.1 root root 9 Mar 16 18:02 python2 -> python2.7

-rwxr-xr-x. 1 root root 7136 Nov 20 2015 python2.7[root@localhost~]#

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

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

将 python 链接至 python3:

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

[root@localhost ~]# ll /usr/bin/ |grep python

lrwxrwxrwx.1 root root 7 Mar 16 18:02 python ->python2

lrwxrwxrwx.1 root root 9 Mar 16 18:02 python2 -> python2.7

-rwxr-xr-x. 1 root root 7136 Nov 20 2015 python2.7[root@localhost~]#

[root@localhost~]# mv /usr/bin/python /usr/bin/python.bak

[root@localhost~]#

[root@localhost~]# ll /usr/bin/ |grep python

lrwxrwxrwx.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

[root@localhost~]#

[root@localhost~]# ln -s /usr/local/bin/python3 /usr/bin/python

[root@localhost~]#

[root@localhost~]# ll /usr/bin/ |grep python

lrwxrwxrwx.1 root root 22 Apr 12 00:24 python -> /usr/local/bin/python3

lrwxrwxrwx.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

[root@localhost~]#

配置YUM

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

[root@localhost ~]# yum list

File"/usr/bin/yum", line 30except KeyboardInterrupt, e:^SyntaxError: invalid syntax

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

# python -V

[root@localhost ~]# python -V

Python3.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值