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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41986507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:01:07  来源:igfitidea点击:

Unable to set default python version to python3 in ubuntu

pythonpython-3.xubuntuinstallationubuntu-16.04

提问by RejeeshChandran

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

我试图将默认的 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

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.

我是 Ubuntu 的新手,我不知道我做错了什么。

采纳答案by Steampunkery

Open your .bashrc file nano ~/.bashrc. Type alias python=python3on 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.

打开您的 .bashrc 文件nano ~/.bashrc。输入alias python=python3到一个新的线在文件的顶部,然后保存使用Ctrl + O和亲近CTRL + X的文件的文件。然后,回到您的命令行输入source ~/.bashrc. 现在您的别名应该是永久的。

EDIT:

编辑:

For update alternatives, the priority is an integer. The priority represents which program should be the first used. Thisarticle sums it all up pretty well.

对于更新替代方案,优先级是一个整数。优先级表示应该首先使用哪个程序。这篇文章总结得很好。

回答by Pardhu

The second line mentioned can be changed to

提到的第二行可以更改为

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

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 .bashrcfile is that it will not work while using the commands with sudo.

这为 的路径提供了 10 的优先级python3。编辑.bashrc文件的缺点是在使用带有sudo.

回答by Purushottam Prabhakar

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

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

Install Python 3.7

安装 Python 3.7

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

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

  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

  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. Update Python 3 to point to Python 3.7

    sudo update-alternatives --config python3Enter 2 for Python 3.7

  2. Test the version of python

  1. 更新 Python 3 以指向 Python 3.7

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

  2. 测试python版本

python3 --v
Python 3.7.1 

回答by DanteVoronoi

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

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

回答by Khan

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

一个简单安全的方法是使用别名。把它放到 ~/.bashrc 文件中:如果你有 gedit 编辑器使用

gedit ~/.bashrc

gedit ~/.bashrc

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

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

alias python=python3

别名 python=python3

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

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

source ~/.bash_aliases or source ~/.bashrc

source ~/.bash_aliases 或 source ~/.bashrc

example:

例子:

$ python --version

Python 2.7.6

$ python3 --version

Python 3.4.3

$ alias python=python3

$ python --version

Python 3.4.3

$ python --version

Python 2.7.6

$ python3 --version

Python 3.4.3

$别名python=python3

$ python --version

Python 3.4.3

回答by Paraic

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

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

alias pip='pip3'

别名 pip='pip3'

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

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

alias pip='pip3.6'
alias python='python3.6'

别名 pip='pip3.6'
别名 python='python3.6'

回答by Paraic

At First Install python3 and pip3

首先安装python3和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版本。

python --version

回答by user7502402

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

正如它所说,update-alternatives --install需要 <link> <name> <path> 和 <priority> 参数。

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

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

update-alternatives --helpsays:

update-alternatives --help说:

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

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

So just put a 100or something at the end

所以只需100在最后放一个或其他东西

回答by Rika

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

保存文件,关闭终端并再次打开它。
你现在应该没事了!关联

回答by pradeep karunathilaka

get python path from

从中获取python路径

ls /usr/bin/python*

then set your python version

然后设置你的python版本

alias python="/usr/bin/python3"