python2.6.6 升级到python2.7.14

来源:互联网 发布:连云港联通网络电视 编辑:程序博客网 时间:2024/06/05 16:07

其实网上有很多关于python2.6.6 升级到python2.7的文章,但是我对比这些类似的文章升级之后,发现其中有错误的地方,于是决定还是自己写一个真正的升级过程。

我的虚拟机里安装的是CentOS 6.5里面默认安装的Python2.6.6,因为要学习TensorFlow,所以决定升级到2.7

1、下载安装包

其实在python官网上有个目录列举出了各个版本的下载安装包,点击这里,可以看到

这里是按照发布时间排列的,往下拉可以找到已经发布的各个版本,我们找到2.7.14,这个是比较重要的一个版本

点击这里的Python-2.7.14.tgz可以直接下载安装包文件,也可以使用wget来直接下载,如果需要安装的服务器无法直接访问外网,可以使用下载安装包文件的方式,如果可以访问外部推荐使用wget方式。

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

无论用哪种方式,下载到安装包之后都会得到Python-2.7.14.tgz这个安装文件

2、解压配置

解压安装文件

tar -zvf Python-2.7.14.tgz  

得到 Python-2.7.14文件夹

cd Python-2.7.14

./configure --prefix=/usr/local/python2.7

执行之后提示是这样

[root@node2 Python-2.7.14]# ./configure --prefix=/usr/local/python2.7checking build system type... x86_64-pc-linux-gnuchecking host system type... x86_64-pc-linux-gnuchecking for python2.7... nochecking for python3... nochecking for python... pythonchecking for --enable-universalsdk... nochecking for --with-universal-archs... 32-bitchecking MACHDEP... linux2checking EXTRAPLATDIR... checking for --without-gcc... nochecking for --with-icc... nochecking for gcc... nochecking for cc... nochecking for cl.exe... noconfigure: error: in `/opt/package/python_lib/Python-2.7.14':configure: error: no acceptable C compiler found in $PATHSee `config.log' for more details

提示:no acceptable C compiler found in $PATH

于是赶紧测试下gcc果然这个不存在,可能是虚拟机在安装CentsOS时没有选择安装GCC

[root@node2 Python-2.7.14]# gcc-bash: gcc: command not found[root@node2 Python-2.7.14]#  yum -y install gcc
执行yum -y install gcc安装了gcc ,重新执行./configure --prefix=/usr/local/python2.7可以正常安装python

执行make

执行make install

然后进入/usr/local/python2.7/bin,这个目录的内容如下:

[root@node2 bin]# ll总用量 6164-rwxr-xr-x. 1 root root     111 11月  9 19:24 2to3-rwxr-xr-x. 1 root root     109 11月  9 19:24 idle-rwxr-xr-x. 1 root root      94 11月  9 19:24 pydoclrwxrwxrwx. 1 root root       7 11月  9 19:27 python -> python2lrwxrwxrwx. 1 root root       9 11月  9 19:27 python2 -> python2.7-rwxr-xr-x. 1 root root 6273995 11月  9 19:24 python2.7-rwxr-xr-x. 1 root root    1697 11月  9 19:27 python2.7-configlrwxrwxrwx. 1 root root      16 11月  9 19:27 python2-config -> python2.7-configlrwxrwxrwx. 1 root root      14 11月  9 19:27 python-config -> python2-config-rwxr-xr-x. 1 root root   18557 11月  9 19:24 smtpd.py[root@node2 bin]# 

3、测试

在这个目录下执行python2或者python2.7都可以出现python的提示符表示安装成功


4、建立软连接

1)备份python2.6.6的启动文件

mv /usr/bin/python /usr/bin/python2.6.6

2)创建用于启动python2.7.14的软连接

ln -s /usr/local/python2.7/bin/python2.7 /usr/bin/python,有的文章里写的是下面这样:ln -s /usr/local/bin/python2.7 /usr/bin/python ,事实证明是有错误的。因为编译好的python2.7.14可执行文件在/usr/local/python2.7/bin/这个目录下面并不在/usr/local/python2.7目录下,同时创建符号链接也需要指定具体的可执行文件名才行。

5.测试

下面我们测试下创建好的软连接

如果软链接的->后面是红色显示的字体,通常表示这个软连接指向的路径是无效的。

此时我们换个其他的目录,比如在/下面执行下python,

可以看到提示的Python2.7.14,证明安装成功,但是还没有彻底结束

6. 配置yum启动路径

yum不兼容 Python 2.7,但是我们现在已经把Python2.6.6升级成了Python2.7.14, 所以yum不能正常工作,我们需要指定 yum 的Python版本,

vim /usr/bin/yum

将头部#!/usr/bin/python 改成#!/usr/bin/python2.6.6(刚刚备份的)

然后保存退出即可。

如果我们想要启动老版本python2.6.6,可以执行python2.6.6即可,如果是想启动python2.7,只需要执行python即可

因此这里的升级不是把python2.6.6彻底干掉,而是重新安装了python2.7.14.而且也兼顾到yum的使用。

原创粉丝点击