如何將 Debian Linux 中的預設的 Python 版本切換為替代版本,link
https://linux.cn/article-6970-1.html
如何將 Debian Linux 中的預設的 Python 版本切換為替代版本
譯者: LCTT Ping Yang| 2016-02-02 20:09 評論: 4 收藏: 8
當你安裝 Debian Linux 時,安裝過程有可能同時為你提供多個可用的 Python 版本,因此係統中會存在多個 Python 的可執行二進位制檔案。你可以按照以下方法使用 ls
命令來檢視你的系統中都有那些 Python 的二進位制檔案可供使用。
$ ls /usr/bin/python*
/usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.4 /usr/bin/python3.4m /usr/bin/python3m
執行如下命令檢視預設的 Python 版本資訊:
$ python --version
Python 2.7.8
1、基於使用者修改 Python 版本:
想要為某個特定使用者修改 Python 版本,只需要在其 home 目錄下建立一個 alias
(別名) 即可。開啟該使用者的 ~/.bashrc
檔案,新增新的別名資訊來修改預設使用的 Python 版本。
alias python='/usr/bin/python3.4'
一旦完成以上操作,重新登入或者重新載入 .bashrc
檔案,使操作生效。
$ . ~/.bashrc
檢查當前的 Python 版本。
$ python --version
Python 3.4.2
2、 在系統級修改 Python 版本
我們可以使用 update-alternatives
來為整個系統更改 Python 版本。以 root 身份登入,首先羅列出所有可用的 python 替代版本資訊:
# update-alternatives --list python
update-alternatives: error: no alternatives for python
如果出現以上所示的錯誤資訊,則表示 Python 的替代版本尚未被 update-alternatives
命令識別。想解決這個問題,我們需要更新一下替代列表,將 python2.7
和 python3.4
放入其中。
# 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
# 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 版本。
# python --version
Python 3.4.2
接下來,我們再次列出可用的 Python 替代版本。
# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4
現在開始,我們就可以使用下方的命令隨時在列出的 Python 替代版本中任意切換了。
# update-alternatives --config python
# python --version
Python 2.7.8
3、移除替代版本
一旦我們的系統中不再存在某個 Python 的替代版本時,我們可以將其從 update-alternatives
列表中刪除掉。例如,我們可以將列表中的 python2.7 版本移除掉。
# update-alternatives --remove python /usr/bin/python2.7
update-alternatives: removing manually selected alternative - switching python to auto mode
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode
via: http://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux