[Linux] 更改 Python 版本

確認命令來源1type command in Linux with Examples

  • type pythonX
  • type -a pythonX (查 alias(別名))

用 update-alternatives 切換

安裝自定選項

sudo update-alternatives --install /usr/bin/pythonX pythonX /usr/bin/pythonX.Y 300

※ 300為優先度,數值越高優先度越高

查看

update-alternatives --display pythonX

指定預設選項

sudo update-alternatives --config pythonX

編輯 alias

編輯 ~/.bashrc

更改 Symbolic Link

#Ubuntu_22_04

  1. 確認 pythonX 的 symbolic link
    1. ls -l /usr/bin | grep pythonX
      → lrwxrwxrwx 1 root root 25 Oct 23 05:43 pythonX -> /etc/alternatives/pythonX
    2. ls -l /etc/alternatives | grep pythonX
      → lrwxrwxrwx 1 root root 19 Oct 23 05:43 pythonX -> /usr/bin/pythonX.Y
    3. 得知/usr/bin/pythonX -> /etc/alternatives/pythonX -> /usr/bin/pythonX.Y
      /usr/bin/pythonX並非實體檔案,故接續下一步
  2. 刪除原本的 pythonX:sudo rm /usr/bin/pythonX
  3. 創建新的 symbolic:sudo ln -s /usr/bin/pythonX.X /usr/bin/pythonX

Last Updated on 2023/10/23 by A1go

References

目錄
Bitnami