我试图在Ubuntu 16.04中将默认的python版本设置为python3。默认值是python2(2.7)。我遵循以下步骤:

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3

但是对于第二个语句,我得到了如下错误,

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs <link> <name> <path> <priority>

Use 'update-alternatives --help' for program usage information.   

当前回答

一个简单安全的方法是使用别名。把这个放到~/中。bashrc文件:( 如果你有gedit编辑器使用

多达gedit / .bashrc

要进入bashrc文件,然后在bashrc文件的顶部进行以下更改。

别名python = python3

在文件中添加上述内容后。运行下面的命令

~ /来源。Bash_aliases或源~/.bashrc

例子:

$ python——version Python 2.7.6 $ python3——version Python 3.4.3 $ alias python=python3 $ python——version Python 3.4.3

其他回答

提到的第二行可以更改为

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10

这为python3的路径提供了10的优先级。

替代编辑.bashrc的缺点是将命令与sudo一起使用将不起作用。

要更改为python3,可以在终端别名python=python3中使用以下命令。

编辑:

我写这篇文章的时候还很年轻很天真,update-alternatives是更好的方法。请看@Pardhu的回答。


过时的回答:

打开你的.bashrc文件nano ~/.bashrc。输入别名python=python3 在文件顶部的新行上,然后按ctrl+o保存文件 并按ctrl+x关闭文件。然后,回到您的命令行类型 ~ / . bashrc来源。现在你的别名应该是永久的了。

只需删除python-is-python2:

sudo apt purge python-is-python2

并安装python-is-python3:

sudo apt install python-is-python3

它将自动转换到新python3的过程。你也可以选择在以后去掉剩下的包:

sudo apt autoremove && sudo apt autoclean

如果有可能直接使用特定的python版本,与更新替代方案和别名解决方案相比,我会选择它。

Ex.

python3.6 -m pip install pytest
ptyhon3.6 -m pytest test_sample.py

-m执行特定python版本的特定模块。 第一行代码将为特定版本和用户在可能的位置/home/user/.local/lib/python3.5/site-packages安装pytest