详解alternatives命令(本例以Python版本切换为例) |
您所在的位置:网站首页 › alternative词缀 › 详解alternatives命令(本例以Python版本切换为例) |
详解alternatives命令(本例以Python版本切换为例)
alternatives是Linux系列操作系统的一个内置命令,即使最小化安装也有该命令,它的主要作用就是版本控制切换,比如,你的系统内有多个Python版本,Python3.8,Python2.7.5,Python3.6,。 首先,应该明确的是,Python可以多个版本共存于一个系统内,因为使用源码编译安装的时候可以指定安装目录,如果是rpm或者yum安装方式,那么,可能会存在版本冲突的问题。并且,Python的各个版本有其自身的特点,比如,Python3这一大版本基本都自带pip包管理器,Python2.7并没有pip,需要自己手动安装。为了加以区分,python3.8的pip 版本为21.0.1,Python2.7.5的pip版本为20.3.4. 环境简介: centos7.2系统,使用自带的Python2.7.5和源码编译安装的Python3.8.1两个版本,Python2.7.5手动安装pip20.3.4,Python由于是源码编译,因此自带pip,安装好Python3.8后升级pip到21.0.1. alternatives命令简介: [root@centos6 piprpm]# alternatives alternatives version 1.7.4 - Copyright (C) 2001 Red Hat, Inc. This may be freely redistributed under the terms of the GNU Public License. usage: alternatives --install [--initscript ] [--family ] [--slave ]* alternatives --remove alternatives --auto alternatives --config alternatives --display alternatives --set alternatives --list common options: --verbose --test --help --usage --version --keep-missing --altdir --admindir主要的常使用的参数是 install ,remove,config,display,list这5个参数。 install --生成软连接 remove --删除软连接 config --选择软连接 display --显示软连接 list --显示所有软连接 假设Python3.8.1已经安装完毕,安装目录指定在 /usr/local/python3.8,Python2.7.5的pip安装的是pip8.1.2,然后升级到pip20.3.4,所有Python和pip都可以正常使用, 首先,将Python3.8.1加入alternatives管理,命令为: alternatives --install /usr/bin/python python /usr/local/python3.8/bin/python3.8 3 该命令将Python3.8加入了alternatives。 其次,将Python2.7.5加入alternatives管理,命令为: alternatives --install /usr/bin/python python /usr/bin/python2.7 2 然后,切换Python的版本到Python3.8 [root@centos6 ~]# alternatives --config python There are 2 programs which provide 'python'. Selection Command ----------------------------------------------- 1 /usr/bin/python2.7 *+ 2 /usr/local/python3.8/bin/python3.8 Enter to keep the current selection[+], or type selection number: 2选择2后即可切换到Python3.8.1版本了。 /usr/bin/python 首先是什么文件呢? [root@centos7 ~]# ls -al /usr/bin/python lrwxrwxrwx. 1 root root 7 Jan 23 22:13 /usr/bin/python -> python2 [root@centos7 bin]# ls -al /usr/bin/python2 lrwxrwxrwx. 1 root root 9 Jan 23 22:13 python2 -> python2.7我们可以看到在未加入alternatives管理前,它是一个链接文件,指向 /usr/bin/python2 而Python2又指向了 /usr/bin/python2.7. 加入alternatives管理后,/usr/bin/python的指向又有所不同了 [root@centos6 ~]# ls -al /usr/bin/python lrwxrwxrwx 1 root root 24 Mar 27 05:33 /usr/bin/python -> /etc/alternatives/python [root@centos6 ~]# ls -al /etc/alternatives/python lrwxrwxrwx 1 root root 34 Mar 27 05:33 /etc/alternatives/python -> /usr/local/python3.8/bin/python3.8这是在切换Python版本到3.8后, /etc/alternatives/python 的指向,可以看到链接到了 /usr/local/python3.8/bin/python3.8。 那么,现在切换到Python2.7.5后呢? [root@centos6 ~]# alternatives --config python There are 2 programs which provide 'python'. Selection Command ----------------------------------------------- 1 /usr/bin/python2.7 *+ 2 /usr/local/python3.8/bin/python3.8 Enter to keep the current selection[+], or type selection number: 2^H There are 2 programs which provide 'python'. Selection Command ----------------------------------------------- 1 /usr/bin/python2.7 *+ 2 /usr/local/python3.8/bin/python3.8 Enter to keep the current selection[+], or type selection number: 1 [root@centos6 ~]# ls -al /usr/bin/python lrwxrwxrwx 1 root root 24 Mar 27 06:50 /usr/bin/python -> /etc/alternatives/python [root@centos6 ~]# ls -al /etc/alternatives/python lrwxrwxrwx 1 root root 18 Mar 27 06:50 /etc/alternatives/python -> /usr/bin/python2.7这里使用 alternatives --config python 后选择的是1,切换到Python2.7了,在看链接可以看到/etc/alternatives/python 指向到了 /usr/bin/python2.7l 。 alternatives --install /usr/bin/python python /usr/local/python3.8/bin/python3.8 3 这条命令到底是怎么写的呢? install 后跟的文件必须是一个链接文件, python是项目名称,/usr/local/python3.8/bin/python3.8是实际文件的绝对路径,3是优先级。第二个命令 设定的优先级是2,表明优先使用Python3.8 这个优先等级高的Python,如果是auto模式的话。 如下图,第二行表示是手动模式,如果是auto,那么会优先使用Python3.8。 第三行说的是现在在使用的是 /usr/bin/python2.7,第四和第五行是说的两个版本的优先级,第六行意思是最好的版本是Python3.8. [root@centos6 ~]# alternatives --display python python - status is manual. link currently points to /usr/bin/python2.7 /usr/bin/python2.7 - priority 2 /usr/local/python3.8/bin/python3.8 - priority 3 Current `best' version is /usr/local/python3.8/bin/python3.8.需要说明的是,Python版本切换后,pip管理器也是跟随自动切换的,即使pip是两个不同的组件。 那么,经过以上的实验,我们可以得出,alternatives类似一个链接管理器,通过 alternatives --config 项目名称,然后选择序号选定使用哪个项目。从而动态的调整链接指向。 alternatives可以用在同一个系统中多gcc,tomcat,jdk版本的情况下,避免造成版本混乱的局面,是一个十分有用的Linux命令。(注意,即使重启系统,只要是manual模式,也仍会保持你的Python版本选择。) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |