linux快速切换默认python版本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012424148/article/details/84966419

1、首先查看当前默认python版本:Python 2.7.6

root@Corleone:/mySoftWare/WxRobot# python -V
Python 2.7.6

2、将默认版本切换为python3.4

我们可以使用update-alternatives为整个系统更改默认python版本。以root用户登录,首先罗列出所有可用python版本信息: 

root@Corleone:/mySoftWare/WxRobot# update-alternatives --list python
update-alternatives: error: no alternatives for python

3、如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被 update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将 python2.7 和 python3.4 放入其中:

root@Corleone:/mySoftWare/WxRobot# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
root@Corleone:/mySoftWare/WxRobot# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

 --install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中。这个例子中,我们为 /usr/bin/python3.4 设置的优先级为2,所以update-alternatives 命令会自动将它设置为默认 Python 版本。再次查看当前默认版本:

root@Corleone:/mySoftWare/WxRobot# python -V
Python 3.4.3

4、罗列可用的Python替代版本

root@Corleone:/mySoftWare/WxRobot# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4

5、可以如下命令快速切换默认版本

update-alternatives --config python
root@Corleone:/mySoftWare/WxRobot# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.4   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.4   2         manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode
root@Corleone:/mySoftWare/WxRobot# python -V
Python 2.7.6
root@Corleone:/mySoftWare/WxRobot# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.4   2         auto mode
* 1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.4   2         manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in manual mode
root@Corleone:/mySoftWare/WxRobot# python -V
Python 3.4.3

6、完整命令代码:

root@Corleone:/mySoftWare/WxRobot# python -V
Python 2.7.6
root@Corleone:/mySoftWare/WxRobot# update-alternatives --list python
update-alternatives: error: no alternatives for python
root@Corleone:/mySoftWare/WxRobot# whereis python
python: /usr/bin/python3.4m /usr/bin/python3.4 /usr/bin/python /usr/bin/python2.7 /etc/python3.4 /etc/python /etc/python2.7 /usr/lib/python3.4 /usr/lib/python2.7 /usr/local/lib/python3.4 /usr/local/lib/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
root@Corleone:/mySoftWare/WxRobot# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
root@Corleone:/mySoftWare/WxRobot# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode
root@Corleone:/mySoftWare/WxRobot# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4
root@Corleone:/mySoftWare/WxRobot# python -V
Python 3.4.3
root@Corleone:/mySoftWare/WxRobot# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.4   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.4   2         manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode
root@Corleone:/mySoftWare/WxRobot# python -V
Python 2.7.6
root@Corleone:/mySoftWare/WxRobot# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.4   2         auto mode
* 1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.4   2         manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in manual mode
root@Corleone:/mySoftWare/WxRobot# python -V
Python 3.4.3


猜你喜欢

转载自blog.csdn.net/u012424148/article/details/84966419