Ubuntu-18.04Python2與Python3自由切換


一、配置ssh鏈接

  • 安裝openssh-server
devops@devops-virtual-machine:~$ sudo apt-get install openssh-server

二、安裝Python3及pip3

devops@devops-virtual-machine:~$ sudo apt-get install python3
devops@devops-virtual-machine:~$ sudo apt install python3-pip

三、將Python3設置為默認

python2切換python3

devops@devops-virtual-machine:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
devops@devops-virtual-machine:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

驗證

devops@devops-virtual-machine:~$ python --version
Python 3.6.5

Python3切換至Python2

devops@devops-virtual-machine:~$ 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)
devops@devops-virtual-machine:~$ python --version
Python 2.7.15rc1

Python2切換至Python3

devops@devops-virtual-machine:~$ 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       手動模式

要維持當前值[*]請按<回車鍵>,或者鍵入選擇的編號:0
update-alternatives: 使用 /usr/bin/python3 來在自動模式中提供 /usr/bin/python (python)
devops@devops-virtual-machine:~$ python --version
Python 3.6.5

 


免責聲明!

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



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