使用update-alternatives在Ubuntu上安裝管理多個Python環境

安裝多個版本的python

1
2
3
4
5
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
sudo apt-get install python3.7
sudo apt-get install python3.8

查看本機安裝的python有哪些

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
knightli@ubuntu:/etc/alternatives$ ls -larth `which python`*
lrwxrwxrwx 1 root root   58 Feb 17  2020 /usr/bin/pythontex -> ../share/texlive/texmf-dist/scripts/pythontex/pythontex.py
lrwxrwxrwx 1 root root   16 Mar 13  2020 /usr/bin/python3-config -> python3.8-config
lrwxrwxrwx 1 root root    9 Mar 13  2020 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root  388 Mar 27  2020 /usr/bin/python3-pasteurize
-rwxr-xr-x 1 root root  384 Mar 27  2020 /usr/bin/python3-futurize
lrwxrwxrwx 1 root root    7 Apr 15  2020 /usr/bin/python -> python2
lrwxrwxrwx 1 root root   33 Jul 28 05:59 /usr/bin/python3.8-config -> x86_64-linux-gnu-python3.8-config
-rwxr-xr-x 1 root root 5.3M Jul 28 05:59 /usr/bin/,python3.8
-rwxr-xr-x 1 root root 3.5M Aug  4 04:16 /usr/bin/python2.7
-rwxr-xr-x 2 root root 4.3M Aug 17 16:45 /usr/bin/python3.6m
-rwxr-xr-x 2 root root 4.3M Aug 17 16:45 /usr/bin/python3.6
-rwxr-xr-x 2 root root 5.3M Aug 17 19:07 /usr/bin/python3.7m
-rwxr-xr-x 2 root root 5.3M Aug 17 19:07 /usr/bin/python3.7
lrwxrwxrwx 1 root root   25 Dec 16 19:39 /usr/bin/python3 -> /etc/alternatives/python3

從以上可以看出一共安裝了 python2.7,python3.6,python3.7,python3.8 四個版本

使用update-alternatives管理多個版本的python

alternatives install python 項目

1
2
3
4
5
6
7
8
knightli@ubuntu:/etc/alternatives$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
knightli@ubuntu:/etc/alternatives$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python (python) in auto mode
knightli@ubuntu:/etc/alternatives$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 3
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in auto mode
knightli@ubuntu:/etc/alternatives$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 4
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in auto mode

添加完以後可以用 alternatives –list 列出安裝的python

1
2
3
4
5
knightli@ubuntu:/etc/alternatives$ sudo update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.6
/usr/bin/python3.7
/usr/bin/python3.8

使用alternatives –config 切換

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
knightli@ubuntu:/etc/alternatives$ sudo update-alternatives --config python
There are 4 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.8   4         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.6   2         manual mode
  3            /usr/bin/python3.7   3         manual mode
  4            /usr/bin/python3.8   4         manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in manual mode
knightli@ubuntu:/etc/alternatives$ python -V
Python 3.7.9

pip 的使用

使用 “python -m pip install xxx " 安裝對應版本的pip

记录并分享
Built with Hugo
Theme Stack designed by Jimmy