我试图在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.   

当前回答

为Linux终端中的默认python设置优先级:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1

这里,我们将python3的优先级设置为10,将python2的优先级设置为1。这将使python3成为默认的python。如果你想将Python2作为默认值,那么将Python2的优先级设置为高于python3

其他回答

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

提到的第二行可以更改为

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

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

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

这是一个简单的方法,适用于我。

sudo ln -s /usr/bin/python3 /usr/bin/python

您可以更改/usr/bin/python3作为python3的路径(或您想要的版本)。

但是请记住,update-alternatives可能是最好的选择。

另一种非侵入性、仅限当前用户的方法是:

# First, make $HOME/bin, which will be automatically added to user's PATH
mkdir -p ~/bin
# make link actual python binaries
ln -s $(which python3) python
ln -s $(which pip3) pip

Python PIP将在一个新的shell中准备好。

如果你有Ubuntu 20.04 LTS (Focal Fossa),你可以安装python-is-python3:

sudo apt install python-is-python3

替换/usr/bin/python中的符号链接指向/usr/bin/python3.