Python 是一种高级编程语言,广泛用于系统编程。它适用于所有流行的操作系统。您可以在单个系统上安装多个 Python 版本。安装多个 Python 版本后,您可以使用 update-alternatives 工具切换默认 Python。
建议所有 Python 开发人员使用虚拟环境对于应用程序。它为具有定义的 Python 版本的应用程序提供了一个隔离的环境。
在 Ubuntu 和 Debian 上切换 Python 版本
update-alternatives 命令行工具用于创建和维护默认命令的符号链接。借助它,我们可以轻松地将命令切换到不同的版本。对于本教程,Python 3.10, and Python 2.7预装在 Debian 系统上。我们将为这两个命令创建一个组并设置符号链接。
- Create a symlink from /usr/bin/python2.7 to /usr/bin/python and set the group name as “python”. Later, group name will be used to switch links.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
Output
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
- Change the symlink link to /usr/bin/python3.10 for /usr/bin/python and set the group name to “python”. The group name must be same for all python versions.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 2
Output
update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python (python) in auto mode
- 重复步骤 2 将更多已安装在您的系统上的 Python 版本添加到组中。
- At this point, You have added two python binary versions to the group name “python”. Now, you can easily switch to any version with the following commands. Here “python” is the group name defined in the above commands.
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.10 2 auto mode
* 1 /usr/bin/python2.7 1 manual mode
2 /usr/bin/python3.10 2 manual mode
Press to keep the current choice[*], or type selection number:
在上面的输出中,Python 将 2.7 设置为当前版本。要将其更改为Python 3.10,您需要输入0 or 2并按回车键。
- That’s it. The current Python version is changed on your system. Just type the following command to view the correctly active Python version.
python -V
Output
Python 3.10.2
您可以将多个 Python 版本添加到一个组中(步骤:01 和 02)并在它们之间轻松切换。
结论
在本教程中,您学习了如何在 Ubuntu 和 Debian Linux 系统上切换默认 Python 版本。除了切换版本之外,您还可以配置Python虚拟环境为您的应用程序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)