LoginSignup
21

More than 3 years have passed since last update.

Raspberry Pi の Python デフォルトバージョンを 2系 から 3系 に切り替える

Posted at

環境

投稿時点での最新の NOOBS をインストール済みです。

$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

切替手順

インストール済 Python バージョン確認

python コマンドで確認すると、2系のPython がデフォルトになっています。

$ python -V
Python 2.7.16

ちなみに 3.7系の Python もインストール済みです。

$ python3 -V
Python 3.7.3

alternatives を確認

皆さんお馴染みの update-alternatives コマンドで、Python がバージョン管理されているか確認します。
もちろんこの時点で Python は登録されていません。

$ sudo update-alternatives --list python
update-alternatives: エラー: python の alternatives がありません

alternatives に登録

update-alternatives コマンドで、Python の 2系(2.7)と3系(3.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 を使います

alternatives を確認

以上の手順で alternative による Python のバージョン管理ができるようになります。

また update-alternatives --config コマンドにより、デフォルトの Python バージョンを確認することができます。
もしこの時点で3系の Python がデフォルトになっていないようなら、選択肢の番号を入力します。(空エンターならそのまま終了)

$ sudo update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.7
$ 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>、さもなければ選択肢の番号のキーを押してください:

念の為、python コマンドで 3系に切り替わっているか確認します。

$ python -V
Python 3.7.3

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
21