Ubuntu18.04は、Pythonのデフォルトバージョン2.7を3.6に変更します

まず、管理者ユーザーの下にsuと入力します。

システムで使用可能なPythonバイナリファイルを確認してください

winter@winter-virtual-machine:~$ ls /usr/bin/python*
/usr/bin/python            /usr/bin/python3.6-config
/usr/bin/python2           /usr/bin/python3.6m
/usr/bin/python2.7         /usr/bin/python3.6m-config
/usr/bin/python2.7-config  /usr/bin/python3-config
/usr/bin/python2-config    /usr/bin/python3m
/usr/bin/python2-qr        /usr/bin/python3m-config
/usr/bin/python3           /usr/bin/python-config
/usr/bin/python3.6

デフォルトのPythonバージョン情報を表示します。

winter@winter-virtual-machine:~$ python --version
Python 2.7.17

update-alternativesを使用して、システム全体のPythonバージョンを変更します。rootとしてログインし、最初に利用可能なすべてのPython代替バージョン情報をリストします。

root@sunqi-virtual-machine:/home/sunqi# update-alternatives --list python
update-alternatives: 错误: 无 python 的候选项

上記のエラーメッセージが表示された場合は、Pythonの代替バージョンがupdate-alternativesコマンドによって認識されていないことを意味します。この問題を解決するには、代替リストを更新してpython2.7とpython3.5を含める必要があります。

root@winter-virtual-machine:/home/winter# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1

root@winter-virtual-machine:/home/winter# update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
update-alternatives: 使用 /usr/bin/python3.6 来在自动模式中提供 /usr/bin/python (python)

-installオプションは、複数のパラメーターを使用してシンボリックリンクを作成します。最後のパラメータは、このオプションの優先度を指定します。代替オプションを手動で設定しない場合は、優先度が最も高いオプションが選択されます。この例では、/ usr / bin / python3.5の優先度を2に設定しているため、update-alternativesコマンドによって自動的にデフォルトのPythonバージョンとして設定されます。

root@winter-virtual-machine:/home/winter# update-alternatives --list python 
/usr/bin/python2.7
/usr/bin/python3.6

次に、Pythonの利用可能な代替バージョンを再度リストします。
今後は、以下のコマンドを使用して、リストされているPythonの代替バージョンをいつでも切り替えることができます。

oot@winter-virtual-machine:/home/winter# update-alternatives --config python
有 2 个候选项可用于替换 python (提供 /usr/bin/python)。

  选择       路径              优先级  状态
------------------------------------------------------------
* 0            /usr/bin/python3.6   2         自动模式
  1            /usr/bin/python2.7   1         手动模式
  2            /usr/bin/python3.6   2         手动模式

要维持当前值[*]请按<回车键>,或者键入选择的编号:2

おすすめ

転載: blog.csdn.net/Zhouzi_heng/article/details/114240524