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

当前回答

首先,确保您的计算机上安装了Python3

回到你的终端,输入:

CD ~/到您的主目录

如果你还没有设置你的.bash_profile,输入touch .bash_profile来创建你的.bash_profile。

或者,输入open -e .bash_profile编辑该文件。

将别名python=python3复制并保存在.bash_profile中。

关闭并重新打开终端。然后输入以下命令来检查Python3是否是你的默认版本:

python的版本

你应该看到python 3.x。Y是默认版本。

干杯!

其他回答

首先,确保您的计算机上安装了Python3

回到你的终端,输入:

CD ~/到您的主目录

如果你还没有设置你的.bash_profile,输入touch .bash_profile来创建你的.bash_profile。

或者,输入open -e .bash_profile编辑该文件。

将别名python=python3复制并保存在.bash_profile中。

关闭并重新打开终端。然后输入以下命令来检查Python3是否是你的默认版本:

python的版本

你应该看到python 3.x。Y是默认版本。

干杯!

只需删除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中使用以下命令。

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

# 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中准备好。

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