mali@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04 LTS (bison-elk-cougar-mlk X54)
Release: 18.04
Codename: bionic
mali@ubuntu:~$ python2 --version
Python 2.7.17
mali@ubuntu:~$ python3 --version
Python 3.6.9
# 表明当前系统的 python 版本为 3.6.9
mali@ubuntu:~$ python --version
Python 3.6.9
mali@ubuntu:~$ which python2
/usr/bin/python2
mali@ubuntu:~$ which python3
/usr/bin/python3
mali@ubuntu:~$
#
mali@ubuntu:~$ echo alias python=python2 >> ~/.bashrc
mali@ubuntu:~$ source ~/.bashrc
mali@ubuntu:~$ python --version
Python 2.7.17
mali@ubuntu:~$ echo alias python=python3 >> ~/.bashrc
mali@ubuntu:~$ source ~/.bashrc
mali@ubuntu:~$ python --version
Python 3.6.9
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
update-alternatives 命令用于处理 linux 系统中软件版本的切换,在各个 linux 发行版中均提供了该命令,命令参数略有区别,但大致是一样的。
mali@ubuntu:~$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 24 6月 28 11:37 /usr/bin/python -> /etc/alternatives/python
mali@ubuntu:~$ ls -l /etc/alternatives/python
lrwxrwxrwx 1 root root 16 6月 28 11:37 /etc/alternatives/python -> /usr/bin/python3
python
这个可执行命令实际是一个链接,指向了 /etc/alternatives/python
。而 /etc/alternatives/python
也是一个链接,指向了 /usr/bin/python3
,这才是最终的可执行文件。alternatives
实际上是通过软链接的方式对版本进行管理。
mali@ubuntu:~$ update-alternatives --help
Usage: update-alternatives [<option> ...] <command>
Commands:
--install <link> <name> <path> <priority>
[--slave <link> <name> <path>] ...
add a group of alternatives to the system.
--remove <name> <path> remove <path> from the <name> group alternative.
--remove-all <name> remove <name> group from the alternatives system.
--auto <name> switch the master link <name> to automatic mode.
--display <name> display information about the <name> group.
--query <name> machine parseable version of --display <name>.
--list <name> display all targets of the <name> group.
--get-selections list master alternative names and their status.
--set-selections read alternative status from standard input.
--config <name> show alternatives for the <name> group and ask the
user to select which one to use.
--set <name> <path> set <path> as alternative for <name>.
--all call --config on all alternatives.
<link> is the symlink pointing to /etc/alternatives/<name>.
(e.g. /usr/bin/pager)
<name> is the master name for this link group.
(e.g. pager)
<path> is the location of one of the alternative target files.
(e.g. /usr/bin/less)
<priority> is an integer; options with higher numbers have higher priority in
automatic mode.
Options:
--altdir <directory> change the alternatives directory.
--admindir <directory> change the administrative directory.
--log <file> change the log file.
--force allow replacing files with alternative links.
--skip-auto skip prompt for alternatives correctly configured
in automatic mode (relevant for --config only)
--verbose verbose operation, more output.
--quiet quiet operation, minimal output.
--help show this help message.
--version show the version.
mali@ubuntu:~$
mali@ubuntu:~$ update-alternatives --display python
python - auto mode
link best version is /usr/bin/python3
link currently points to /usr/bin/python3
link python is /usr/bin/python
/usr/bin/python3 - priority 10
mali@ubuntu:~$
mali@ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2
[sudo] password for mali:
mali@ubuntu:~$ update-alternatives --display python
python - auto mode
link best version is /usr/bin/python3
link currently points to /usr/bin/python3
link python is /usr/bin/python
/usr/bin/python2.7 - priority 2
/usr/bin/python3 - priority 10
mali@ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
mali@ubuntu:~$ update-alternatives --display python
python - manual mode
link best version is /usr/bin/python2
link currently points to /usr/bin/python3
link python is /usr/bin/python
/usr/bin/python2 - priority 100
/usr/bin/python2.7 - priority 2
/usr/bin/python3 - priority 10
mali@ubuntu:~$ python --version
Python 2.7.17
mali@ubuntu:~$ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python2 100 auto mode
1 /usr/bin/python2 100 manual mode
2 /usr/bin/python2.7 2 manual mode
* 3 /usr/bin/python3 10 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode
mali@ubuntu:~$ python --version
Python 2.7.17
mali@ubuntu:~$ update-alternatives --display python
python - auto mode
link best version is /usr/bin/python2
link currently points to /usr/bin/python2
link python is /usr/bin/python
/usr/bin/python2 - priority 100
/usr/bin/python2.7 - priority 2
/usr/bin/python3 - priority 10
mali@ubuntu:~$
mali@ubuntu:~$ ll /usr/bin/python*
lrwxrwxrwx 1 root root 24 6月 28 11:37 /usr/bin/python -> /etc/alternatives/python*
lrwxrwxrwx 1 root root 9 1月 9 2020 /usr/bin/python2 -> python2.7*
-rwxr-xr-x 1 root root 3637096 4月 16 01:20 /usr/bin/python2.7*
lrwxrwxrwx 1 root root 33 4月 16 01:20 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config*
lrwxrwxrwx 1 root root 16 4月 16 2018 /usr/bin/python2-config -> python2.7-config*
-rwxr-xr-x 1 root root 385 4月 12 2018 /usr/bin/python2-futurize*
-rwxr-xr-x 1 root root 389 4月 12 2018 /usr/bin/python2-pasteurize*
lrwxrwxrwx 1 root root 9 10月 25 2018 /usr/bin/python3 -> python3.6*
-rwxr-xr-x 2 root root 4526456 4月 18 09:56 /usr/bin/python3.6*
lrwxrwxrwx 1 root root 33 4月 18 09:56 /usr/bin/python3.6-config -> x86_64-linux-gnu-python3.6-config*
-rwxr-xr-x 2 root root 4526456 4月 18 09:56 /usr/bin/python3.6m*
lrwxrwxrwx 1 root root 34 4月 18 09:56 /usr/bin/python3.6m-config -> x86_64-linux-gnu-python3.6m-config*
-rwxr-xr-x 2 root root 4873376 11月 7 2019 /usr/bin/python3.7*
-rwxr-xr-x 2 root root 4873376 11月 7 2019 /usr/bin/python3.7m*
lrwxrwxrwx 1 root root 16 10月 25 2018 /usr/bin/python3-config -> python3.6-config*
lrwxrwxrwx 1 root root 10 10月 25 2018 /usr/bin/python3m -> python3.6m*
lrwxrwxrwx 1 root root 17 10月 25 2018 /usr/bin/python3m-config -> python3.6m-config*
lrwxrwxrwx 1 root root 16 4月 16 2018 /usr/bin/python-config -> python2.7-config*