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

当前回答

       ~$ 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

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

回到你的终端,输入:

CD ~/到您的主目录

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

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

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

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

python的版本

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

干杯!

首先安装python3和pip3

sudo apt-get install python3 python3-pip

然后在终端运行

alias python=python3

检查机器中的python版本。

python --version
sudo rm /usr/bin/python3 #remove existing link
sudo ln /usr/bin/python3.8 /usr/bin/python3 # create a new link to the version of your choice

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

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