使用 update-alternatives 在 python3.7 和 python3.8 之间切换不起作用

使用 update-alternatives 在 python3.7 和 python3.8 之间切换不起作用

我希望能够在 python3.8 和 python3.7 以及较新版本的 python 之间切换。现在如何将系统默认 python 版本设置为 python3.7?我以为可以使用 update-alternatives 完成。我正在运行 ubuntu 20.04.4

欢迎任何帮助。

干杯

❯ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.8   5         auto mode
* 1            /usr/bin/python3.7   4         manual mode
  2            /usr/bin/python3.8   5         manual mode

Press <enter> to keep the current choice[*], or type selection number: ^C
❯ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 24 May 14 05:37 /usr/bin/python -> /etc/alternatives/python
❯ ssh [email protected]
❯ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.8   5         auto mode
* 1            /usr/bin/python3.7   4         manual mode
  2            /usr/bin/python3.8   5         manual mode

Press <enter> to keep the current choice[*], or type selection number:
❯ which python
python: aliased to python3
❯ whereis python
python: /usr/bin/python2.7 /usr/bin/python /usr/bin/python3.8-config /usr/bin/python3.7m /usr/bin/python3.7 /usr/bin/python3.8 /usr/lib/python2.7 /usr/lib/python3.9 /usr/lib/python3.7 /usr/lib/python3.8 /etc/python2.7 /etc/python3.7 /etc/python3.8 /usr/local/lib/python2.7 /usr/local/lib/python3.7 /usr/local/lib/python3.8 /usr/include/python3.8 /usr/share/python
❯ ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Mar 13  2020 /usr/bin/python3 -> python3.8

❯ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

答案1

即使 update-alternatives 有效,也不要更改 python 的默认版本。它可能会破坏您的系统。

在 bashrc 中使用别名可能会更好

在文本编辑器中打开 $HOME/.bashrc 并附加

alias python=python3.7

这只会更改终端上的 python 版本,不会影响依赖于它的系统程序。

相关内容