LoginSignup
0
2

More than 1 year has passed since last update.

UbuntuやRaspbianでPython3をデフォルトにするDebian的方法

Last updated at Posted at 2021-06-13

現在配布されているRaspbianOSなど主要なUbuntuから派生したディストリビューションは今もPhyton2.7がデフォルトでPython3を使うには明示的にpython3とコマンドを打たなければいけないことが多いです。普段の用途ではpythonいえばpython3なので、pythonコマンドでpython3を起動したいところです。Ubuntuを含むDebian系では複数のコマンドが一つのコマンド名を使う時の調停の仕組みとしてupdate-alternativeというシステムが用意されていますが、なぜか/usr/bin/pythonだけはこの仕組みを利用していないようなので、alternativeを使った方法を紹介します。おそらくDebian的により正式な方法です

普通にpythonコマンドのalternativeを変更しようとすると以下のように出てきます。

$ sudo update-alternatives --list python
update-alternatives: error: no alternatives for python

エラーにひるまずalternativeを設定します。

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 10
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 20

あとはupdate-alternatives --config pythonをするとpythonのデフォルトを聞かれるようになります。

$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3   20        auto mode
  1            /usr/bin/python2   10        manual mode
  2            /usr/bin/python3   20        manual mode

pipは自動的に追随してくれるようです

0
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2