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の合計4つのバージョンがインストールされています。

update-alternatives を使用した Python の複数バージョンの管理

代替案は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

代替の –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

ピップの使用

「python -m pip install xxx」を使用して、対応するバージョンの pip をインストールします。

记录并分享
Hugo で構築されています。
テーマ StackJimmy によって設計されています。