在 Debian 里“设置 Python 默认版本”要分情况,因为系统自带很多脚本依赖 /usr/bin/python(或 python3),乱改可能破坏系统。下面按推荐程度说明。
update-alternatives(推荐)假设你想用 Python 3.10:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 2
切换版本:
sudo update-alternatives --config python
选择对应编号即可。
✅ 优点:
⚠️ 注意:
不要改 /usr/bin/python3,那个是系统用的
echo "alias python='python3.11'" >> ~/.bashrc
source ~/.bashrc
✅ 只影响当前用户
❌ 对脚本 #!/usr/bin/python 无效
python3.11 -m venv venv
source venv/bin/activate
✅ 不碰系统
✅ 项目隔离
❌ 直接覆盖:
sudo ln -sf /usr/bin/python3.11 /usr/bin/python
可能导致:
python --version
python3 --version
ls /usr/bin/python*
Debian 不保证
/usr/bin/python存在
新代码应明确写python3
如果你告诉我:
我可以给你最合适的一条命令方案。