无法在ubuntu中将默认python版本设置为python3

问题:无法在ubuntu中将默认python版本设置为python3

我试图将默认python版本设置为python3in Ubuntu 16.04。默认情况下为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的新手,我不知道自己在做什么错。

I was trying to set default python version to python3 in Ubuntu 16.04. By default it is python2 (2.7). I followed below steps :

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3

but I’m getting the following error for the second statement,

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.   

I’m new to Ubuntu and Idon’t know what I’m doing wrong.


回答 0

打开您的.bashrc文件nano ~/.bashrcalias python=python3在文件顶部的新行上键入,然后使用ctrl + o保存文件并使用ctrl + x关闭文件。然后,返回您的命令行类型source ~/.bashrc。现在,您的别名应该是永久的。

编辑:

对于更新替代方案,优先级是整数。优先级表示应该首先使用哪个程序。文章总结说起来相当不错。

EDIT:

I wrote this when I was young an naive, update-alternatives is the better way to do this. See @Pardhu’s answer.

Open your .bashrc file nano ~/.bashrc. Type alias python=python3 on to a new line at the top of the file then save the file with ctrl+o and close the file with ctrl+x. Then, back at your command line type source ~/.bashrc. Now your alias should be permanent.


回答 1

提到的第二行可以更改为

update-alternatives --install /usr/bin/python python /usr/bin/python3 10

的路径优先级为10 python3。编辑.bashrc文件的缺点是,当与结合使用命令时,它将不起作用sudo

更新:sudo在运行以下命令时使用:

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

The second line mentioned can be changed to

update-alternatives --install /usr/bin/python python /usr/bin/python3 10

This gives a priority of 10 for the path of python3. The disadvantage of editing .bashrc file is that it will not work while using the commands with sudo.

Update: Please use sudo while running the command like this:

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


回答 2

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

安装Python 3.7

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

  1. 使用apt-get安装python3.7软件包

    sudo apt-get install python3.7

  2. 将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
  1. 更新Python 3以指向Python 3.7

    sudo update-alternatives --config python3 为Python 3.7输入2

  2. 测试python版本

python3 --v
Python 3.7.1 

To change Python 3.6.8 as the default in Ubuntu 18.04 to Python 3.7.

Install Python 3.7

Steps to install Python3.7 and configure it as the default interpreter.

  1. Install the python3.7 package using apt-get

    sudo apt-get install python3.7

  2. Add Python3.6 & Python 3.7 to 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
  1. Update Python 3 to point to Python 3.7

    sudo update-alternatives --config python3 Enter 2 for Python 3.7

  2. Test the version of python

python3 --version
Python 3.7.1 

回答 3

要更改为python3,可以在terminal中使用以下命令alias python=python3

To change to python3, you can use the following command in terminal alias python=python3.


回答 4

一种简单安全的方法是使用别名。将其放入〜/ .bashrc文件中:如果您使用gedit编辑器

gedit〜/ .bashrc

进入bashrc文件,然后在bashrc文件的顶部进行以下更改。

别名python = python3

在文件中添加以上内容之后。运行以下命令

源〜/ .bash_aliases或源〜/ .bashrc

例:

$ python-版本

Python 2.7.6

$ python3-版本

Python 3.4.3

$别名python = python3

$ python-版本

Python 3.4.3

A simple safe way would be to use an alias. Place this into ~/.bashrc file: if you have gedit editor use

gedit ~/.bashrc

to go into the bashrc file and then at the top of the bashrc file make the following change.

alias python=python3

After adding the above in the file. run the below command

source ~/.bash_aliases or source ~/.bashrc

example:

$ python –version

Python 2.7.6

$ python3 –version

Python 3.4.3

$ alias python=python3

$ python –version

Python 3.4.3


回答 5

另外,您还可以为pip添加一个别名(在.bashrc或bash_aliases中):

别名pip =’pip3′

许多人发现python3的全新安装实际上指向python3.x,因此您可能需要:

别名pip =’pip3.6′
别名python =’python3.6′

As an added extra, you can add an alias for pip as well (in .bashrc or bash_aliases):

alias pip=’pip3′

You many find that a clean install of python3 actually points to python3.x so you may need:

alias pip=’pip3.6′
alias python=’python3.6′


回答 6

首先安装python3和pip3

sudo apt-get install python3 python3-pip

然后在终端运行

alias python=python3

检查计算机中的python版本。

python --version

At First Install python3 and pip3

sudo apt-get install python3 python3-pip

then in your terminal run

alias python=python3

Check the version of python in your machine.

python --version

回答 7

如果您具有Ubuntu Focal(20.20),则可以安装python-is-python3

sudo apt-get install python-is-python3

替换符号链接/usr/bin/python以指向/usr/bin/python3.8

If you have Ubuntu 20.04 LTS (Focal Fossa) you can install python-is-python3:

sudo apt install python-is-python3

which replaces the symlink in /usr/bin/python to point to /usr/bin/python3.


回答 8

就像说的那样 update-alternatives --install需要<link> <name> <path>和<priority>参数。

您具有链接(/usr/bin/python),名称(python)和路径(/usr/bin/python3),但缺少优先级。

update-alternatives --help 说:

<priority>是整数;数字更大的选项在自动模式下具有更高的优先级。

所以100在末尾放一个

As it says, update-alternatives --install needs <link> <name> <path> and <priority> arguments.

You have link (/usr/bin/python), name (python), and path (/usr/bin/python3), you’re missing priority.

update-alternatives --help says:

<priority> is an integer; options with higher numbers have higher priority in automatic mode.

So just put a 100 or something at the end


回答 9

cd ~
gedit .bash_aliases

然后写

alias python=python3

要么

alias python='/usr/bin/python3'

保存文件,关闭终端,然后再次打开。
您现在应该很好!链接

Do

cd ~
gedit .bash_aliases

then write either

alias python=python3

or

alias python='/usr/bin/python3'

Save the file, close the terminal and open it again.
You should be fine now! Link


回答 10

只需按照以下步骤操作,即可将默认python更改为新升级的python版本。对我来说很好。

  • sudo apt-install python3.7 安装所需的最新版本的python
  • cd /usr/bin 输入安装了python的根目录
  • sudo unlink pythonsudo unlink python3。取消链接当前的默认python
  • sudo ln -sv /usr/bin/python3.7 python 链接新下载的python版本
  • python --version 检查新的python版本,您一切顺利

Just follow these steps to help change the default python to the newly upgrade python version. Worked well for me.

  • sudo apt-install python3.7 Install the latest version of python you want
  • cd /usr/bin Enter the root directory where python is installed
  • sudo unlink python or sudo unlink python3 . Unlink the current default python
  • sudo ln -sv /usr/bin/python3.7 python Link the new downloaded python version
  • python --version Check the new python version and you’re good to go

回答 11

对于另一种仅限于当前用户的非侵入式方法:

# 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 将在新的外壳中准备好。

For another non-invasive, current-user only approach:

# 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 will be ready in a new shell.


回答 12

从获取python路径

ls /usr/bin/python*

然后设置你的python版本

alias python="/usr/bin/python3"

get python path from

ls /usr/bin/python*

then set your python version

alias python="/usr/bin/python3"

回答 13

适用于所有用户的ubuntu 18.04中的最佳方法是

sudo vim /etc/bash.bashrc
add lines
alias python=python3
alias pip=pip3

保存更改并重新启动。

重新启动后,系统中的python 3版本以及python 2.7都将作为默认版本。如果您使用的是python 3的多个版本,则可以通过在别名中说以下内容来更加具体。

sudo vim /etc/bash.bashrc
add lines
alias python=python3.6
alias pip=pip3.6

The best way in ubuntu 18.04 which will work for all users is

sudo vim /etc/bash.bashrc
add lines
alias python=python3
alias pip=pip3

Save the changes and restart .

After restart what ever version of python 3 you have in the system along with python 2.7 will be taken as default. You could be more specific by saying the following in alias if you have multiple version of python 3.

sudo vim /etc/bash.bashrc
add lines
alias python=python3.6
alias pip=pip3.6

回答 14

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

Simply remove python-is-python2:

sudo apt purge python-is-python2

And install python-is-python3:

sudo apt install python-is-python3

It will automate the process of transition to new python3. Optionally you can get rid of remaining packages later:

sudo apt autoremove && sudo apt autoclean

回答 15

要将Python 3.6.8从Python 2.7更改为Ubuntu 18.04中的默认设置,可以尝试使用命令行工具update-alternatives

sudo update-alternatives --config python

如果出现错误“ python没有替代品”,请使用以下命令自己设置替代品:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

相应地将路径更改为/usr/bin/python3所需的python版本。

优先级指定的最后一个参数表示,如果未进行手动替代选择,则将设置优先级最高的替代项。在我们的示例中,我们为设置了优先级2,/usr/bin/python3.6.8因此/usr/bin/python3.6.8update-alternatives命令自动将其设置为默认python版本。

我们可以随时使用以下命令在上面列出的python替代版本之间切换,并输入选择编号:

update-alternatives --config python

To change Python 3.6.8 as the default in Ubuntu 18.04 from Python 2.7 you can try the command line tool update-alternatives.

sudo update-alternatives --config python

If you get the error “no alternatives for python” then set up an alternative yourself with the following command:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Change the path /usr/bin/python3 to your desired python version accordingly.

The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python3.6.8 and as a result the /usr/bin/python3.6.8 was set as default python version automatically by update-alternatives command.

we can anytime switch between the above listed python alternative versions using below command and entering a selection number:

update-alternatives --config python

回答 16

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

转到终端并输入:

cd ~/ 转到您的主目录

如果尚未设置,请.bash_profile键入touch .bash_profile以创建.bash_profile。

或者,键入open -e .bash_profile以编辑文件。

复制并保存alias python=python3在.bash_profile中。

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

python --version

您应该看到python 3.xy是您的默认版本。

干杯!

At first, Make sure Python3 is installed on your computer

Go to your terminal and type:

cd ~/ to go to your home directory

If you didn’t set up your .bash_profile yet, type touch .bash_profile to create your .bash_profile.

Or, type open -e .bash_profile to edit the file.

Copy and save alias python=python3 in the .bash_profile.

Close and reopen your Terminal. Then type the following command to check if Python3 is your default version now:

python --version

You should see python 3.x.y is your default version.

Cheers!