码迷,mamicode.com
首页 > 编程语言 > 详细

centos7升级Python2.x到3.x

时间:2019-04-11 16:16:22      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:syn   比较   ks.cfg   pytho   ons   setup.py   syntax   执行   共存   

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

一、查看当前Python版本

# python -V

[root@localhost ~]# python -V
Python 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

[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::223
Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 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]

[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.in  README.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
total 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.ac
drwxr-xr-x. 18 501 501   4096 Oct  3  2017 Doc
drwxr-xr-x.  2 501 501     20 Oct  3  2017 Grammar
drwxr-xr-x.  2 501 501   4096 Oct  3  2017 Include
-rwxr-xr-x.  1 501 501   7122 Oct  3  2017 install-sh
drwxr-xr-x. 33 501 501   8192 Oct  3  2017 Lib
-rw-r--r--.  1 501 501  12757 Oct  3  2017 LICENSE
drwxr-xr-x.  8 501 501   4096 Oct  3  2017 Mac
-rw-r--r--.  1 501 501  59746 Oct  3  2017 Makefile.pre.in
drwxr-xr-x.  2 501 501   4096 Oct  3  2017 Misc
drwxr-xr-x. 13 501 501   4096 Oct  3  2017 Modules
drwxr-xr-x.  4 501 501   4096 Oct  3  2017 Objects
drwxr-xr-x.  2 501 501   4096 Oct  3  2017 Parser
drwxr-xr-x.  6 501 501   4096 Oct  3  2017 PC
drwxr-xr-x.  2 501 501   4096 Oct  3  2017 PCbuild
drwxr-xr-x.  2 501 501     79 Oct  3  2017 Programs
-rw-r--r--.  1 501 501  41453 Oct  3  2017 pyconfig.h.in
drwxr-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.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: cant decompress data; zlib not available
make: *** [install] Error 1

 

验证

[root@localhost ~]# python -V
Python 2.7.5
[root@localhost ~]# python3 -V
Python 3.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.7
lrwxrwxrwx.   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.7
lrwxrwxrwx.   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 30
    except 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
Python 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

标签:syn   比较   ks.cfg   pytho   ons   setup.py   syntax   执行   共存   

原文地址:https://www.cnblogs.com/djlsunshine/p/10689591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!