在 Linux 中,update-alternatives 是一个用来管理一组特定命令(例如 Python)的多个可能版本的系统工具。
使用 update-alternatives 在多个 python 版本之间进行切换的步骤如下:
- 列出所有可选的 python 版本:
ls /usr/bin/python*
这会显示出所有可用的python版本。 - 为每一个版本创建一个备选项,例如:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
这里,我们要使用 update-alternatives 来注册可用的 Python 版本。后面的数字 1 和 2 是优先级,当 Python 版本冲突时,数字大的版本会被优先使用。 - 使用 update-alternatives 切换 python 版本,例如:
sudo update-alternatives --config python
运行此命令后,会列出你添加的所有 Python 版本,然后你可以按照提示,输入相应的选择编号切换到你想要使用的 Python 版本。
记住,这会更改系统范围内的 python 版本。因此,如果你在处理一个需要特定版本的 Python 的项目,更好的办法是使用虚拟环境(如:venv 或 conda)。