Ubuntu18.04 modify the Python default version 2.7 to 3.6

First enter su under the administrator user.

Check to see which Python binary files are available on your system

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

View the default Python version information:

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

Use update-alternatives to change the Python version for the entire system. Log in as root, first list all available python alternative version information:

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

If the error message shown above appears, it means that the alternative version of Python has not been recognized by the update-alternatives command. To solve this problem, we need to update the alternative list to include python2.7 and 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)

The -install option uses multiple parameters to create symbolic links. The last parameter specifies the priority of this option. If we do not manually set the alternative option, then the option with the highest priority will be selected. In this example, we set the priority for /usr/bin/python3.5 to 2, so the update-alternatives command will automatically set it as the default Python version.

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

Next, we again list the available alternative versions of Python.
From now on, we can use the commands below to switch between the listed alternative versions of Python at any time.

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

Guess you like

Origin blog.csdn.net/Zhouzi_heng/article/details/114240524