Linux下如何切换多版本Python

环境介绍:

操作系统:CentOS 7

正文:

在升级httpd后,由于某些RPM版本较低,导致启动失败。因此需要用yum升级,当执行升级命令时,出现如下错误:

[root@~]# yum install -y db4-devel expat-devel
Loaded plugins: fastestmirror
Determining fastest mirrors
 * epel: abqix.mm.fcix.net
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax


Exiting on user cancel

这个错误之前遇到过,第一时间就想到是python版本不对导致的。yum使用的python2.x,当前默认的python版本是3.x,如下图:

[root@ ~]# python
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

一种简单粗暴的方法,就是直接将python的bin文件指向2.x版本。以下是搜索python得到的文件路径:

[root@ ~]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/bin/python3.6 /usr/bin/python3.6m /usr/lib/python2.7 /usr/lib/python3.6 /usr/lib64/python3.6 /usr/lib64/python2.7 /etc/python /usr/local/lib/python3.6 /usr/include/python2.7 /usr/include/python3.6m /usr/share/man/man1/python.1.gz

我们到/usr/bin目录下,看看python的具体情况:

[root@~ /]# cd /usr/bin
[root@~ bin]# ll | grep python
lrwxrwxrwx  1 root root          9 Mar 20 17:52 python -> python3.6
lrwxrwxrwx. 1 root root          9 Aug 14  2017 python2 -> python2.7
-rwxr-xr-x. 1 root root       7136 Nov  6  2016 python2.7
-rwxr-xr-x  1 root root      11328 Apr 23  2021 python3
-rwxr-xr-x  2 root root      11328 Nov 17  2020 python3.6
-rwxr-xr-x  2 root root      11328 Nov 17  2020 python3.6m
lrwxrwxrwx. 1 root root          7 Aug 14  2017 python_old27 -> python2

由上面的内容可以看出,python文件是一个软链接,它链接的是python3.6的文件。所以,我们可以直接修改python的软链接指向2.7的文件即可。

上面的方法就比较简单粗暴,下面介绍一种安装更友好的方法,使用update-alternatives命令来管理多版本的问题。

以下是man update-alternatives命令输出的内容:

UPDATE-ALTERNATIVES(8)                                                                  System Manager's Manual                                                                  UPDATE-ALTERNATIVES(8)

NAME
       alternatives - maintain symbolic links determining default commands

SYNOPSIS
       alternatives [options] --install link name path priority [--slave link name path]...  [--initscript service] [--family name]

       alternatives [options] --remove name path

       alternatives [options] --set name path

       alternatives [options] --auto name

       alternatives [options] --display name

       alternatives [options] --config name

       alternatives [options] --list name

所以,我们可以通过以下命令,对python进行多版本管理:

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 100
update-alternatives --install /usr/bin/python python /usr/bin/python3.6 110

# 参数介绍
# 第一个参数: --install 添加软链接。
# 第二个参数: 软链接的地址。
# 第三个参数: 软链接的名字。
# 第四个参数: 被管理的命令绝对路径。
# 第五个参数: 优先级,数字越大优先级越高。

设置成功之后,可使用以下命令来切换不同的版本了:

update-alternatives --config python

执行上述命令,会出现如下内容:

[root@~]# update-alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
 + 1           /usr/bin/python2.7
*  2           /usr/bin/python3.6

Enter to keep the current selection[+], or type selection number:
[root@ ~]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

通过输入数字,可以切换不同的版本。

我们可以再次查看/usr/bin下python的目录情况:

[root@~ bin]# ll |grep python
lrwxrwxrwx    1 root root         24 Apr  2 18:54 python -> /etc/alternatives/python
lrwxrwxrwx.   1 root root          9 Aug 14  2017 python2 -> python2.7
-rwxr-xr-x.   1 root root       7136 Nov  6  2016 python2.7
-rwxr-xr-x    1 root root      11328 Apr 23  2021 python3
-rwxr-xr-x    2 root root      11328 Nov 17  2020 python3.6
-rwxr-xr-x    2 root root      11328 Nov 17  2020 python3.6m
lrwxrwxrwx.   1 root root          7 Aug 14  2017 python_old27 -> python2

可以看到,此时/usr/bin/python已经被指向了/etc/alternatives/python,查看/etc/alternatives/python,得到如下结果:

[root@ bin]# ll /etc/alternatives/python
lrwxrwxrwx 1 root root 18 Apr  2 18:54 /etc/alternatives/python -> /usr/bin/python3.6

/etc/alternatives/python也是一个软链接,其指向 /usr/bin/python3.6。由此看来,update-alternatives命令和我们前面讲的直接修改软链接的粗暴方式并无二异。

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值