切换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 版本。

posted @ 2024-05-25 16:28  Jack_Q  阅读(19)  评论(0编辑  收藏  举报