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

一、查看当前Python版本

# python -V

[root@localhost ~]# python -V
Python 2.7.

二、下载新的Python包并安装

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

# yum install gcc gcc-c++ -y

Updated:
gcc.x86_64 :4.8.-.el7_6. gcc-c++.x86_64 :4.8.-.el7_6. Dependency Updated:
cpp.x86_64 :4.8.-.el7_6. gcc-gfortran.x86_64 :4.8.-.el7_6. libgcc.x86_64 :4.8.-.el7_6.
libgfortran.x86_64 :4.8.-.el7_6. libgomp.x86_64 :4.8.-.el7_6. libquadmath.x86_64 :4.8.-.el7_6.
libquadmath-devel.x86_64 :4.8.-.el7_6. libstdc++.x86_64 :4.8.-.el7_6. libstdc++-devel.x86_64 :4.8.-.el7_6. 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
---- ::-- 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::
Connecting to www.python.org (www.python.org)|151.101.228.223|:... connected.
HTTP request sent, awaiting response... OK
Length: (22M) [application/octet-stream]
Saving to: ‘Python-3.6..tgz’ %[===================================================================================================================>] ,, .7KB/s in 3m 23s -- :: ( KB/s) - ‘Python-3.6..tgz’ saved [/] [root@localhost ~]#
[root@localhost ~]# ls
anaconda-ks.cfg Python-3.6..tgz
[root@localhost ~]#
[root@localhost ~]# du -sh Python-3.6..tgz
22M Python-3.6..tgz

解压

# tar zxvf Python-3.6.3.tgz

[root@localhost ~]# ls
anaconda-ks.cfg Python-3.6. Python-3.6..tgz
[root@localhost ~]#

查看文件内容

[root@localhost ~]# cd Python-3.6.
[root@localhost Python-3.6.]#
[root@localhost Python-3.6.]# 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.]#
[root@localhost Python-3.6.]# ll
total
-rw-r--r--. Oct aclocal.m4
-rwxr-xr-x. Oct config.guess
-rwxr-xr-x. Oct config.sub
-rwxr-xr-x. Oct configure
-rw-r--r--. Oct configure.ac
drwxr-xr-x. Oct Doc
drwxr-xr-x. Oct Grammar
drwxr-xr-x. Oct Include
-rwxr-xr-x. Oct install-sh
drwxr-xr-x. Oct Lib
-rw-r--r--. Oct LICENSE
drwxr-xr-x. Oct Mac
-rw-r--r--. Oct Makefile.pre.in
drwxr-xr-x. Oct Misc
drwxr-xr-x. Oct Modules
drwxr-xr-x. Oct Objects
drwxr-xr-x. Oct Parser
drwxr-xr-x. Oct PC
drwxr-xr-x. Oct PCbuild
drwxr-xr-x. Oct Programs
-rw-r--r--. Oct pyconfig.h.in
drwxr-xr-x. Oct Python
-rw-r--r--. Oct README.rst
-rw-r--r--. Oct setup.py
drwxr-xr-x. Oct Tools

# ./configure

[root@localhost Python-3.6.]# ./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

验证

[root@localhost ~]# python -V
Python 2.7.
[root@localhost ~]# python3 -V
Python 3.6.

设置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. root root Mar : python -> python2
lrwxrwxrwx. root root Mar : python2 -> python2.
-rwxr-xr-x. root root Nov python2.
[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. root root Mar : python -> python2
lrwxrwxrwx. root root Mar : python2 -> python2.
-rwxr-xr-x. root root Nov python2.
[root@localhost ~]#
[root@localhost ~]# mv /usr/bin/python /usr/bin/python.bak
[root@localhost ~]#
[root@localhost ~]# ll /usr/bin/ | grep python
lrwxrwxrwx. root root Mar : python2 -> python2.
-rwxr-xr-x. root root Nov python2.
lrwxrwxrwx. root root Mar : 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. root root Apr : python -> /usr/local/bin/python3
lrwxrwxrwx. root root Mar : python2 -> python2.
-rwxr-xr-x. root root Nov python2.
lrwxrwxrwx. root root Mar : python.bak -> python2
[root@localhost ~]#

配置YUM

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

[root@localhost ~]# yum list
File "/usr/bin/yum", line
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.

参考博客:

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的更多相关文章

  1. CentOS7 升级 Python2.x 到 Python3.x

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

  2. CentOS7 升级Python2.x到3.x

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

  3. centos7 升级python2.7 到python3.6(Centos7 安装Anaconda)

    Anaconda 下载 https://www.anaconda.com/download/#linux 下载文件 Anaconda3-5.2.0-Linux-x86_64.sh bash Anaco ...

  4. 装python package 时,conda提示会升级python2到python3,那可能是你的windows不支持py2env下的此包。

    装python package 时,conda提示会升级python2到python3, 那可能是你的windows不支持py2env下的此包.比如:win 下,tensorflow就不支持py2的环 ...

  5. linux 升级python2.7

    linux为centos6,系统默认安装了python2.6,需要执行的python脚本内容包含标准库之xml.etree.ElementTree  用到库里的一个iter方法是python2.7的新 ...

  6. 【转】Mac下升级python2.7到python3.6

    1. 前言 Mac系统自带python2.7,本文目的是将自带的python升级到3.6版本. 网上有本多的做法是让python2.7和python3.X两个版本共存,博主并不知道,是两版本共存好,还 ...

  7. CentOS6升级Python2.6到3.7,错误处理[No module named '_ctypes']

    CentOS6升级Python2.6到3.7,错误处理[No module named '_ctypes'] 因开发需要,在CentOS 6 服务器将Python2进行升级到Python3.由于工作中 ...

  8. 转载: Centos7 升级python3,解决升级后不兼容问题

    Centos7配置更新国内yum源 http://blog.csdn.net/qingguiyu/article/details/50721956 Centos7 升级python3,解决升级后不兼容 ...

  9. CentOS6.x 安装升级Python2.7.x Python3.4.x

    CentOS6.x 安装升级Python2.7.x Python3.4.x 2015-06-07• CentOS.Linux • 评论关闭 CentOS release 6.6 (Final) 安装升 ...

随机推荐

  1. 在Visual Studio 2017中安装bower

    在项目目录下添加一个文件.bowerrc { "directory": "wwwroot/lib" } JS包默认安装到webroot的lib文件夹,可以通过. ...

  2. linux 使用split分割大文件

    1.分割 -- split命令 可以指定按行数分割和按字节大小分割两种模式. (1) 按行数分割 $ split -l 300 large_file.txt new_file_prefix 加上-d, ...

  3. Mysql怎么判断繁忙 checkpoint机制 innodb的主要参数

    Mysql怎么判断繁忙,innodb的主要参数,checkpoint机制,show engine innodb status   2018年07月13日 15:45:36 anzhen0429 阅读数 ...

  4. Linux下安装Julia1.0.0

    MIT正式发布编程语言Julia 1.0:Python.R.C++三合一! 由于官方文档提示中安装的不是最新的Julia版本,官方的0.7版本安装方法为:                       ...

  5. centos打开matlab的正确姿势

    进入usr/local/MATLAB/R2015b/bin ./matlab

  6. red hat防火墙的开启与关闭及状态查看方法

    Redhat使用了SELinux来增强安全, 首先怎么查看防火墙的状态呢? a.可以通过如下命令查看iptables防火墙状态: chkconfig --list iptables b. selinu ...

  7. 黑客炼金术士 Seeker:可以攻破 4G 摸到你短信,还要为朝阳群众提供谍战工具

    在北京上地的一家咖啡馆里,我在等待黑客 Seeker 的到来. 我对黑客 Seeker 颇有期待.他曾在黑客大会 KCon 上演讲<伪基站高级利用技术——彻底攻破短信验证码>,介绍利用 L ...

  8. FCC JS基础算法题(12):Where do I belong(数组排序并找出元素索引)

    题目描述: 先给数组排序,然后找到指定的值在数组的位置,最后返回位置对应的索引.举例:where([1,2,3,4], 1.5) 应该返回 1.因为1.5插入到数组[1,2,3,4]后变成[1,1.5 ...

  9. single-cell RNA-seq 工具大全

    [怪毛匠子-整理] awesome-single-cell List of software packages (and the people developing these methods) fo ...

  10. 【软件安装】nvidia驱动安装事宜

    https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html https://docs.nvidia.com/cuda/arch ...