1. 查看當前系統有哪些版本
$ ls /usr/bin/python*
2、基於用戶修改 Python 版本
想要為某個特定用戶修改 Python 版本,只需要在其 home 目錄下創建一個 alias(別名) 即可。打開該用戶的 ~/.bashrc 文件,添加新的別名信息來修改默認使用的 Python 版本。
alias python='/usr/bin/python3.5'
一旦完成以上操作,重新登錄或者重新加載 .bashrc 文件,使操作生效。
$ . ~/.bashrc
檢查當前的 Python 版本。
$ python --version
Python 3.5
3、 在系統級修改 Python 版本
使用 update-alternatives 來為整個系統更改 Python 版本。以 root 身份登錄,首先羅列出所有可用的 python 替代版本信息:
# update-alternatives --list python
update-alternatives: error: no alternatives for python
如果出現以上所示的錯誤信息,則表示 Python 的替代版本尚未被 update-alternatives 命令識別。想解決這個問題,我們需要更新一下替代列表,將 python2.7 和 python3.5放入其中。
# 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.5
update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python (python) in auto mode
--install 選項使用了多個參數用於創建符號鏈接。最后一個參數指定了此選項的優先級,如果我們沒有手動來設置替代選項,那么具有最高優先級的選項就會被選中。
使用下方的命令隨時在列出的 Python 替代版本中任意切換了。
# update-alternatives --config python
4、移除替代版本
一旦我們的系統中不再存在某個 Python 的替代版本時,我們可以將其從 update-alternatives 列表中刪除掉.如移除python2.7
# update-alternatives --remove python /usr/bin/python2.7