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

当前回答

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

sudo apt install python-is-python3

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

其他回答

只需删除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

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

如果有可能直接使用特定的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

将Ubuntu 18.04中的默认Python 3.6.8更改为Python 3.7。

安装Python 3.7

安装Python3.7并将其配置为默认解释器的步骤。

使用apt-get安装python3.7包 Sudo apt-get install python3.7 将Python3.6和Python 3.7添加到update-alternatives中

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2

更新Python 3以指向Python 3.7 Sudo update-alternatives——config python3 Python 3.7输入2 测试python版本

python3 --version
Python 3.7.1 

提到的第二行可以更改为

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

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

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