1. 程式人生 > >linux快速切換預設python版本

linux快速切換預設python版本

1、首先檢視當前預設python版本:Python 2.7.6

[email protected]:/mySoftWare/WxRobot# python -V
Python 2.7.6

2、將預設版本切換為python3.4

我們可以使用update-alternatives為整個系統更改預設python版本。以root使用者登入,首先羅列出所有可用python版本資訊: 

[email protected]:/mySoftWare/WxRobot# update-alternatives --list python
update-alternatives: error: no alternatives for python

3、如果出現以上所示的錯誤資訊,則表示 Python 的替代版本尚未被 update-alternatives 命令識別。想解決這個問題,我們需要更新一下替代列表,將 python2.7 和 python3.4 放入其中:

[email protected]:/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
[email protected]
:/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 版本。再次檢視當前預設版本:

[email protected]:/mySoftWare/WxRobot# python -V
Python 3.4.3

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

[email protected]:/mySoftWare/WxRobot# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4

5、可以如下命令快速切換預設版本

update-alternatives --config python
[email protected]:/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
[email protected]:/mySoftWare/WxRobot# python -V
Python 2.7.6
[email protected]:/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
[email protected]:/mySoftWare/WxRobot# python -V
Python 3.4.3

 

6、完整命令程式碼:

[email protected]:/mySoftWare/WxRobot# python -V
Python 2.7.6
[email protected]:/mySoftWare/WxRobot# update-alternatives --list python
update-alternatives: error: no alternatives for python
[email protected]:/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
[email protected]:/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
[email protected]:/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
[email protected]:/mySoftWare/WxRobot# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4
[email protected]:/mySoftWare/WxRobot# python -V
Python 3.4.3
[email protected]:/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
[email protected]:/mySoftWare/WxRobot# python -V
Python 2.7.6
[email protected]:/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
[email protected]:/mySoftWare/WxRobot# python -V
Python 3.4.3