UbuntuでPythonのバージョンを管理するための一般的な手順は以下の通りです。
-
現在のPythonのバージョンを確認します。
bash
$ python --version -
インストールされているPythonのバージョンとその場所を確認します。
bash
$ which python #すべてのpythonがインストールされている場所を確認
$ ls /usr/bin/ | grep python #/usr/bin/以下にインストールされている全pythonのバージョンを確認 -
update-alternatives
が存在することを確認します。これはPythonのバージョン管理を行います。
bash
$ which update-alternatives -
Pythonのバージョンを登録します。以下のコマンドはPython3.6と2.7を登録する例です。
bash
$ 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のバージョンやリストの順、優先順位を確認します。
bash
$ update-alternatives --config python
以上の手順でPythonのバージョンを切り替えることができます。これにより、特定のプロジェクトで必要なPythonのバージョンを簡単に切り替えることができます。ただし、システム全体のPythonバージョンを変更すると予期しない問題が発生する可能性があるため、注意が必要です。.