Ubuntu-18.04Python2與Python3自由切換

1、配置ssh連接

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

2、安裝Python3及pip3

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

3、將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
相關文章
相關標籤/搜索