Ubuntu 下 Python自由切換


sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

切換

# sudo update-alternatives --config python
有 2 個候選項可用於替換 python (提供 /usr/bin/python)。

  選擇       路徑            優先級  狀態
------------------------------------------------------------
  0            /usr/bin/python3   150       自動模式
* 1            /usr/bin/python2   100       手動模式
  2            /usr/bin/python3   150       手動模式

要維持當前值[*]請按<回車鍵>,或者鍵入選擇的編號:2
update-alternatives: 使用 /usr/bin/python3 來在手動模式中提供 /usr/bin/python (python)
$ python
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
$ sudo update-alternatives --config python
有 2 個候選項可用於替換 python (提供 /usr/bin/python)。

  選擇       路徑            優先級  狀態
------------------------------------------------------------
  0            /usr/bin/python3   150       自動模式
  1            /usr/bin/python2   100       手動模式
* 2            /usr/bin/python3   150       手動模式

要維持當前值[*]請按<回車鍵>,或者鍵入選擇的編號:1
update-alternatives: 使用 /usr/bin/python2 來在手動模式中提供 /usr/bin/python (python)
$ python
Python 2.7.15 (default, Nov 12 2018, 14:31:15) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

移除

$ sudo update-alternatives --remove python /usr/bin/python2
update-alternatives: 正在移除手動選擇的候選項 - 切換 python 到自動模式
update-alternatives: 使用 /usr/bin/python3 來在自動模式中提供 /usr/bin/python (python)
$ sudo update-alternatives --config python
鏈接組 python (提供 /usr/bin/python)中只有一個候選項:/usr/bin/python3
無需配置。

經測試,重啟之后,依然生效。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM