Ubuntu下 Python 版本切换

在Ubuntu的开发环境下,由于Python2和Python3很多不兼容,经常会需要我们手动切换Python版本。

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

如果想再切回python2.7 ,可以使用命令:

sudo update-alternatives --config python

运行python --version 查看当前使用的python版本

例如查看当前使用的是Python2:

$ python --version
Python 2.7.17

可以使用下述命令切换至Python3:

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
[sudo] password for ts:
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode
$ python --version
Python 3.6.9

想再切换至Python2,可以使用下述命令:

$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3   150       auto mode
  1            /usr/bin/python2   100       manual mode
  2            /usr/bin/python3   150       manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2 to provide /usr/bin/python (python) in manual mode

$ python --version
Python 2.7.17

你可能感兴趣的:(Ubuntu,shell,ubuntu,linux)