我试图在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 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 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中准备好。

       ~$ sudo apt-get install python3.9
/usr/bin$ cd /usr/bin
/usr/bin$ sudo unlink python3
/usr/bin$ sudo ln -sv /usr/bin/python3.9 python3
/usr/bin$ python3 --version
          Python 3.9.5
/usr/bin$ pip3 --version
          pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)

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

编辑:

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


过时的回答:

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