Pythonのバージョンを切り替えるためには、まず現在のPythonのバージョンを確認します。

$ python --version
Python 2.7.17

次に、インストールされているPythonのバージョンとその場所を確認します。

$ which python    #すべてのpythonがインストールされている場所を確認
/usr/bin/python    #/usr/bin以下にあるようです
$ ls /usr/bin/ | grep python    #/usr/bin/以下にインストールされている全pythonのバージョンを確認
python2 python2.7 python3 python3.6 python3.6-config python3-config

update-alternativesが存在することを確認します。

$ which update-alternatives
/usr/bin/update-alternatives

次に、Pythonのバージョンを登録します。

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1    #python3.6を優先順位1で登録
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2    #python2.7を優先順位2で登録

登録されたPythonのバージョンやリストの順、優先順位を確認します。

$ update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).
Selection    Path                Priority   Status
------------------------------------------------------------
0            /usr/bin/python2.7   2         auto mode
* 1            /usr/bin/python2.7   2         manual mode
2            /usr/bin/python3.6   1         manual mode
Press <enter> to keep the current choice[*], or type selection number: 2    #python3.6を使用したいのでSelectionの2を入力
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python (python) in manual mode
$ python --version    #使用する設定になっているpythonのバージョンを確認
Python 3.6.9

以上の手順でPythonのバージョンを切り替えることができます。.

投稿者 admin

コメントを残す

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