离线升级python2.7至最新版

前言

因为客户的环境不支持联网升级,因此只能离线升级。升级过程中遇到一些小问题,边学边解决,遂以此记录,以供不时之需。

正文

升级之前,查看python的版本:

[root@localhost ~]# python -V
Python 2.7.5
[root@localhost ~]#

从上面的输出得知,当前环境运行的python版本是2.7.5。因为安全漏洞的原因,我们直接将其升级到当前大版本(2)的最新版本。最新版可在pkgs.org网站上去查询,支持多个操作系统以及多个版本。

查看当前环境使用的操作系统:

[root@localhost ~]# cat /etc/system-release
CentOS Linux release 7.3.1611 (Core)
[root@localhost ~]#

因此,操作系统是centos 7,通过 pkgs.org 网站,查询到python2.7的最新版本是:2.7.18,包名:python27-python-2.7.18-4.el7.x86_64.rpm(点击链接可下载)。

在python27-python-2.7.18-4.el7.x86_64.rpm的描述界面上,描述了安装rpm包需要如下文件:
在这里插入图片描述
由于不知道系统还缺少哪些文件,因此,我们可以先执行一下python包的rpm安装命令,看下输出结果,就知道还缺少哪些文件了。因为之前已经安装了python的低版本,所以,这里我们用-U参数,表示升级操作,vh 表示输出详情:

[root@localhost python_rpm]# rpm -Uvh python27-python-2.7.18-4.el7.x86_64.rpm
error: Failed dependencies:
        python27-python-libs(x86-64) = 2.7.18-4.el7 is needed by python27-python-2.7.18-4.el7.x86_64
        python27-python-pip is needed by python27-python-2.7.18-4.el7.x86_64
        python27-python-setuptools is needed by python27-python-2.7.18-4.el7.x86_64
        python27-runtime is needed by python27-python-2.7.18-4.el7.x86_64

从上面的输出,得知,需要以下几个文件:

python27-python-libs(x86-64) = 2.7.18-4.el7
python27-python-pip
python27-python-setuptools
python27-runtime

可直接通过前述 Requires 界面找到以上四个文件的rpm包。这里就不展开描述,找到文件,保存,然后上传至服务器,再次执行升级操作。

[root@localhost python_rpm]# rpm -Uvh python*.rpm
warning: python27-python-pip-8.1.2-7.el7.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID f2ee9d55: NOKEY
error: Failed dependencies:
        scl-utils is needed by python27-runtime-1.1-26.1.el7.x86_64

对比前面的命令,这次执行升级命令有些许不同,为什么要这么写呢?你可以试着单独升级这四个依赖包,比如升级python27-python-libs:

[root@localhost python_rpm]# rpm -Uvh python27-python-libs-2.7.18-4.el7.x86_64.rpm
error: Failed dependencies:
        python27-python(abi) = 2.7 is needed by python27-python-libs-2.7.18-4.el7.x86_64
        python27-runtime is needed by python27-python-libs-2.7.18-4.el7.x86_64

输出提示缺少python27-python(abi) = 2.7 依赖包,这个依赖包在哪里呢?答案是在python27-python-2.7.18-4.el7.x86_64.rpm包里,可参考python27-python-2.7.18-4.el7.x86_64.rpm的描述界面上,“Provides ” 一栏有描述该rpm包安装后提供的文件。

通过上面的描述,是不是发现了循环依赖的问题?python的依赖包又依赖python本身。所以,解决办法就是将所有依赖包都和python的安装包放在同一个目录下,一并升级,也可以将所有包名都挨个列出来执行,我这里使用通配符的方式更简洁:

[root@localhost python_rpm]# rpm -Uvh python*.rpm
warning: python27-python-pip-8.1.2-7.el7.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID f2ee9d55: NOKEY
error: Failed dependencies:
        scl-utils is needed by python27-runtime-1.1-26.1.el7.x86_64

上述内容表示还缺少 scl-utils 模块,参考前面的步骤,找到 scl-utils 依赖包,下载,然后安装:

[root@localhost python_rpm]# rpm -Uvh scl-utils-20130529-19.el7.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:scl-utils-20130529-19.el7        ################################# [100%]
[root@localhost python_rpm]#

重新执行python的升级操作:

[root@localhost python_rpm]# rpm -Uvh python*.rpm
warning: python27-python-pip-8.1.2-7.el7.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID f2ee9d55: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:python27-runtime-1.1-26.1.el7    ################################# [ 20%]
   2:python27-python-libs-2.7.18-4.el7################################# [ 40%]
   3:python27-python-pip-8.1.2-7.el7  ################################# [ 60%]
   4:python27-python-setuptools-0.9.8-################################# [ 80%]
   5:python27-python-2.7.18-4.el7     ################################# [100%]
[root@localhost python_rpm]#

至此,升级成功,查看python版本是否已变化:

[root@localhost python_rpm]# python -V
Python 2.7.5
[root@localhost python_rpm]#

额,版本号没变。还差一步,执行以下命令:

[root@localhost python_rpm]# scl enable python27 bash
[root@localhost python_rpm]#

再次查看版本:

[root@localhost python_rpm]# python -V
Python 2.7.18
[root@localhost python_rpm]#

通过上述输出,表明版本已升级成功。为什么要执行 scl enable python27 bash 命令呢,他们表示什么意思?

Software collections(SCLs)是一个软件多版本共存的解决方案。

SCL不修改已安装的软件版本,也不会与其产生冲突。以下是官网的描述:
在这里插入图片描述
因为最新的rpm包采用的SCL方式,所以升级之后,必须执行SCL命令,否则系统默认还是执行的老的版本。

升级后的文件,都安装在目录 /opt/rh/python27 下:

[root@localhost python27]# ll
total 4
-rw-r--r--  1 root root 451 Jun 22  2018 enable
dr-xr-xr-x 17 root root 224 Oct 24 17:15 root

打开 enable 文件,可以看到如下内容:在这里插入图片描述
上面的内容其实就是重新设置python的环境变量,所以,我们也可以不用SCL命令,使用

source ./enable

也可以达到相同的效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值