Ubuntu16.04切换python2和python3
2018-05-17 本文已影响0人
从0到1的小姐姐
装了ubuntu16.04之后,本来默认有两个版本的python,一般默认都是python2.7,但是我已经忘了啥时候设置成了默认的python3.5,现在我想要换回2.7,因为有的命令3.5和2.7不一致,很麻烦
1.切换到/usr/bin目录下,然后查看所有可用的python版本
cd /usr/bin
ls |grep python
![](https://img.haomeiwen.com/i7517555/62f5fba034bdd8ac.png)
2.查看默认python版本,我的是3.5
python
![](https://img.haomeiwen.com/i7517555/d3861db6f73b04b0.png)
3.使用update-alternatives来为整个系统更改Python版本的优先级
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2
--install选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中
这个例子中,我们为/usr/bin/python2.7设置的优先级为2,所以update-alternatives命令会自动将它设置为默认Python版本
![](https://img.haomeiwen.com/i7517555/c56424021bea1151.png)
python
已经能看到被默认设置为python2.7了![](https://img.haomeiwen.com/i7517555/a9723c94f4fb8b80.png)
4.设置为手动选择
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 1
update-alternatives --list python
update-alternatives --config python
![](https://img.haomeiwen.com/i7517555/888f989e73d27368.png)
update-alternatives --config python
会让你选择序号![](https://img.haomeiwen.com/i7517555/09de5978f7e8457c.png)
Permission denied
,这时就要在刚刚的命令前加上sudo
变为sudo update-alternatives --config python
,然后再做选择,会发现python版本已经可以成功切换了 ,美滋滋!![](https://img.haomeiwen.com/i7517555/d62dc20ae43714cb.png)