Raspberry Pi OS/Debian/Ubuntuでの既定のPython切り替え

最近使うPythonアプリはPython3が多くなっています.Python2のEoLが迫っているので正しいのですが,手元の環境では python コマンドは python2 に向いています.python3 コマンドを叩けばいいのですが,これを python3 に向けられないかなと試してみました.

Raspberry Pi OS arm64(busterベース)でのPython確認してみます.python コマンドは python2.7 を呼ぶようになっています.

$ python --version
Python 2.7.16
$ ls -l `which python`
lrwxrwxrwx 1 root root 7  3月  5  2019 /usr/bin/python -> python2
$ ls -l `which python2`
lrwxrwxrwx 1 root root 9  3月  5  2019 /usr/bin/python2 -> python2.7
$ ls -l `which python3`
lrwxrwxrwx 1 root root 9  3月 26  2019 /usr/bin/python3 -> python3.7

update-alternatives で管理されてるのかな?と思いましたが設定がなさそうです.

$ update-alternatives --get-selections|grep -i ^python

update-alternatives を手動で設定してみます.
これで python コマンドが python3.7 を呼ぶようになりました.

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: /usr/bin/python (python) を提供するために自動モードで /usr/bin/python2.7 を使います
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
update-alternatives: /usr/bin/python (python) を提供するために自動モードで /usr/bin/python3.7 を使います
$ ls -l `which python`
lrwxrwxrwx 1 root root 24  7月 28 08:47 /usr/bin/python -> /etc/alternatives/python
$ python --version
Python 3.7.3
$ update-alternatives --query python
Name: python
Link: /usr/bin/python
Status: auto
Best: /usr/bin/python3.7
Value: /usr/bin/python3.7

Alternative: /usr/bin/python2.7
Priority: 1

Alternative: /usr/bin/python3.7
Priority: 2

切り替えたいときはこんな感じで選択肢なおせばok.

$ sudo update-alternatives --config python
alternative python (/usr/bin/python を提供) には 2 個の選択肢があります。

  選択肢    パス              優先度  状態
------------------------------------------------------------
* 0            /usr/bin/python3.7   2         自動モード
  1            /usr/bin/python2.7   1         手動モード
  2            /usr/bin/python3.7   2         手動モード

現在の選択 [*] を保持するには <Enter>、さもなければ選択肢の番号のキーを押してください:

Debian asid amd64, Ubuntu 20.04 LTS amd64 でも設定してみました.

Debian sid amd64
$ update-alternatives --query python
Name: python
Link: /usr/bin/python
Status: auto
Best: /usr/bin/python3.8
Value: /usr/bin/python3.8

Alternative: /usr/bin/python2.7
Priority: 1

Alternative: /usr/bin/python3.7
Priority: 2

Alternative: /usr/bin/python3.8
Priority: 3
Ubuntu 20,04 LTS amd64
$ update-alternatives --query python
Name: python
Link: /usr/bin/python
Status: auto
Best: /usr/bin/python3.8
Value: /usr/bin/python3.8

Alternative: /usr/bin/python2.7
Priority: 1

Alternative: /usr/bin/python3.5
Priority: 2

Alternative: /usr/bin/python3.8
Priority: 3

てことで python コマンドが python3 になりました.しかしシステムワイドな設定なのでシステムのプログラムなどで問題が出るかもしれないですね.
問題が起こったらpython2に戻しましょう.

Rasoberry Pi OS amd64
$ dpkg-query -W python python3
python  2.7.16-1
python3 3.7.3-1
$ lsb_release -dr
Description:    Debian GNU/Linux 10 (buster)
Release:        10
$ uname -m
aarch64
$ cat /proc/device-tree/model&&echo
Raspberry Pi 3 Model B Rev 1.2
Debian sid amd64
$ dpkg-query -W python python3 python3.7
python  2.7.17-2
python3 3.8.2-3
python3.7       3.7.7-1+b1
$ lsb_release -dr
Description:    Debian GNU/Linux bullseye/sid
Release:        unstable
$ uname -m
x86_64
Ubuntu 20.04 LTS amd64
$ dpkg-query -W python python3 python3.7
python  2.7.17-1
python3 3.8.2-0ubuntu2
python3.7
$ lsb_release -dr
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
$ uname -m
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)