1. 程式人生 > >Ubuntu16.04切換python版本

Ubuntu16.04切換python版本

ubuntu16.04中預設已經安裝了python2.7和python3.5

[email protected]:/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.怎麼隨意切換

[email protected]:/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,再次檢視如下 [email protected]:/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: