linux快速切换默认python版本

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


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幻欢子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值