Linux 升级 Python 至 3.x

查看 Python 版本号 python

当 Linux 上安装 Python 后(默认安装),只须要输入简单的命令,就能够查看 Python 的版本号: c++

  1. # python -V
  2. Python 2.7.5

或者是: python2.7

  1. # python --version
  2. Python 2.7.5

 

下载新版本

进入 Python下载页面,选择须要的版本。 url

https://www.python.org/ftp/python/ spa

  1. wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

 

解压缩

下载完成以后,进行解压缩: get

  1. # tar -zxvf Python-3.5.2.tgz

 

安装配置

进入解压缩后的目录,安装配置: 编译器

  1. # cd Python-3.5.2/
  2. # ./configure

 

执行 ./configure 时,若是报错: it

  1. configure: error: no acceptable C compiler found in $PATH

 

说明没有安装合适的编译器。这时,须要安装/升级 gcc 及其它依赖包。 io

  1. # yum install make gcc gcc-c++

完成以后,从新执行: 编译

  1. # ./configure

 

编译 & 安装

配置完成以后,就能够编译了:

  1. # make

完成后,安装:

  1. # make install

 

验证

安装成功之后,就能够查看 Python 的版本了:

  1. # python -V
  2. Python 2.7.5
  3. # python3 -V
  4. Python 3.5.2

 

一个是旧版本 2.x,另一个是新版本 3.x。

注意:在 /usr/local/bin/ 下有一个 python3 的连接,指向 bin 目录下的 python 3.5

设置 3.x 为默认版本

查看 Python 的路径,在 /usr/bin 下面。能够看到 python 连接的是 python 2.7,因此,执行 python 就至关于执行 python 2.7。

  1. # ls -al /usr/bin | grep python
  2. -rwxr-xr-x. 1 root root 11216 12月 1 2015 abrt-action-analyze-python
  3. lrwxrwxrwx. 1 root root 7 8月 30 12:11 python -> python2
  4. lrwxrwxrwx. 1 root root 9 8月 30 12:11 python2 -> python2.7
  5. -rwxr-xr-x. 1 root root 7136 11月 20 2015 python2.7

 

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

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

将 python 连接至 python3:

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

这时,再查看 Python 的版本:

  1. # python -V
  2. Python 3.5.2

 

配置 yum

升级 Python 以后,因为将默认的 python 指向了 python3,yum 不能正常使用,须要编辑 yum 的配置文件:

  1. # vi /usr/bin/yum

同时修改:

  1. # vi /usr/libexec/urlgrabber-ext-down

将 #!/usr/bin/python 改成 #!/usr/bin/python2.7,保存退出便可。

相关文章
相关标签/搜索