切换python3 版本
在 Ubuntu 上安装了多个 Python 版本后,你可以使用 `update-alternatives` 命令来管理和切换默认的 Python 版本。以下是具体步骤:
### 使用 `update-alternatives` 切换 Python 版本
1. **添加 Python 3.12 到 `update-alternatives` 系统:**
```bash
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2
```
这里的 `1` 和 `2` 是优先级,数值越大优先级越高。
2. **配置 `update-alternatives` 选择默认版本:**
```bash
sudo update-alternatives --config python3
```
运行这条命令后,你会看到一个选择菜单,可以选择你想要的默认 Python 版本。例如:
```
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python3.12 2 auto mode
1 /usr/bin/python3.10 1 manual mode
2 /usr/bin/python3.12 2 manual mode
Press <enter> to keep the current choice[*], or type selection number:
```
输入相应的编号,然后按 `Enter` 键。
3. **验证切换结果:**
```bash
python3 -V
```
现在你应该会看到 Python 3.12 的版本信息。
### 其他方法:使用别名
如果你不想使用 `update-alternatives`,也可以通过设置别名来临时切换 Python 版本。
1. **编辑 `~/.bashrc` 文件:**
```bash
nano ~/.bashrc
```
2. **添加别名:**
在文件末尾添加以下行:
```bash
alias python3='/usr/bin/python3.12'
```
3. **应用更改:**
```bash
source ~/.bashrc
```
4. **验证切换结果:**
```bash
python3 -V
```
你应该会看到 Python 3.12 的版本信息。
通过以上方法,你可以在 Ubuntu 系统中方便地切换不同的 Python 版本。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现