Ubuntu16.04切换python版本

本文介绍在Ubuntu16.04中如何在预装的Python2.7与Python3.5间进行版本切换的方法。通过使用`update-alternatives`命令可以轻松地在不同版本间切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ubuntu16.04中默认已经安装了python2.7和python3.5

hanli@ubuntu:/usr/bin$ ls |grep python
dh_python2
dh_python3
python
python2
python2.7
python2.7-config
python2-config
python3
python3.5
python3.5m
python3m
python-config
x86_64-linux-gnu-python2.7-config
x86_64-linux-gnu-python-config
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

怎么切换?

1.现在是2.7

hanli@ubuntu:/usr/bin$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
  • 1
  • 2

2.执行两条命令

hanli@ubuntu:/usr/bin$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
update-alternatives: using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode
hanli@ubuntu:/usr/bin$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode
  • 1
  • 2
  • 3
  • 4

3.再次查看已经变成3.5了

hanli@ubuntu:/usr/bin$ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
  • 1
  • 2

4.怎么随意切换

hanli@ubuntu:/usr/bin$ 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

#数字前面有*号表示是当前使用版本,输入1切换到2.7,再次查看如下

hanli@ubuntu:/usr/bin$ 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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值