简体   繁体   English

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

[英]Unable to set default python version to python3 in ubuntu

I was trying to set default python version to python3 in Ubuntu 16.04 .我试图在Ubuntu 16.04中将默认 python 版本设置为python3 By default it is python2 (2.7).默认情况下它是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.   

The second line mentioned can be changed to提到的第二行可以更改为

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

This gives a priority of 10 for the path of python3 .这为python3的路径提供了 10 的优先级。

The disadvantage of alternatively editing .bashrc is that using the commands with sudo will not work.交替编辑.bashrc的缺点是使用带有sudo的命令将不起作用。

EDIT:编辑:

I wrote this when I was young and naive, update-alternatives is the better way to do this.我在年轻和天真时写了这篇文章, update-alternatives是更好的方法。 See @Pardhu's answer.请参阅@Pardhu 的回答。


Outdated answer:过时的答案:

Open your .bashrc file nano ~/.bashrc .打开你的 .bashrc 文件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.在文件顶部的新行中键入alias python=python3然后使用 ctrl+o 保存文件并使用 ctrl+x 关闭文件。 Then, back at your command line type source ~/.bashrc .然后,回到你的命令行类型source ~/.bashrc Now your alias should be permanent.现在您的别名应该是永久的。

To change Python 3.6.8 as the default in Ubuntu 18.04 to Python 3.7.将 Python 3.6.8 作为 Ubuntu 18.04 中的默认设置更改为 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使用apt-get安装python3.7包

    sudo apt-get install python3.7

  2. Add Python3.6 & Python 3.7 to update-alternatives将 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更新 Python 3 以指向 Python 3.7

    sudo update-alternatives --config python3 Enter 2 for Python 3.7 sudo update-alternatives --config python3对于 Python 3.7 输入 2

  2. Test the version of python测试python的版本

python3 --version
Python 3.7.1 

If you have Ubuntu 20.04 LTS (Focal Fossa) you can install python-is-python3 :如果你有 Ubuntu 20.04 LTS (Focal Fossa) 你可以安装python-is-python3

sudo apt install python-is-python3

which replaces the symlink in /usr/bin/python to point to /usr/bin/python3 .它将/usr/bin/python中的符号链接替换为指向/usr/bin/python3

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

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 $ python3 --版本

Python 3.4.3

$ alias python=python3 $ 别名 python=python3

$ python --version $蟒蛇--版本

Python 3.4.3

Update:更新:
Since Ubuntu 20.04, the python3 is the default version, but still, python is not registered as python3 by default.从 Ubuntu 20.04 开始,python3 是默认版本,但是,python 默认没有注册为 python3。 In order to make that happen, you can simply do :为了实现这一点,您可以简单地执行以下操作:

sudo apt install python-is-python3

For more information you can check this out.有关更多信息,您可以查看内容。
Old way:老办法:

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关联

Just follow these steps to help change the default python to the newly upgrade python version.只需按照以下步骤帮助将默认 python 更改为新升级的 python 版本。 Worked well for me.对我来说效果很好。

  • sudo apt-install python3.7 Install the latest version of python you want sudo apt-install python3.7安装你想要的最新版python
  • cd /usr/bin Enter the root directory where python is installed cd /usr/bin进入安装python的根目录
  • sudo unlink python or sudo unlink python3 . sudo unlink pythonsudo unlink python3 Unlink the current default python取消链接当前默认的python
  • sudo ln -sv /usr/bin/python3.7 python Link the new downloaded python version sudo ln -sv /usr/bin/python3.7 python链接新下载的python版本
  • python --version Check the new python version and you're good to go python --version检查新的python版本,你很高兴

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

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'别名 pip='pip3.6'
alias python='python3.6'别名 python='python3.6'

This is a simple way that works for me.这是一种对我有用的简单方法。

sudo ln -s /usr/bin/python3 /usr/bin/python

You could change /usr/bin/python3 for your path to python3 (or the version you want).您可以更改/usr/bin/python3为您的 python3 路径(或您想要的版本)。

But keep in mind that update-alternatives is probably the best choice.但请记住, update-alternatives可能是最好的选择。

As it says, update-alternatives --install needs <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 --help says: update-alternatives --help说:

<priority> is an integer; <priority> 是一个整数; options with higher numbers have higher priority in automatic mode.数字越大的选项在自动模式下的优先级越高。

So just put a 100 or something at the end所以只要在最后放一个100或其他东西

get python path from获取python路径

ls /usr/bin/python*

then set your python version然后设置你的python版本

alias python="/usr/bin/python3"

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 .要将 Python 3.6.8 从 Python 2.7 更改为 Ubuntu 18.04 中的默认值,您可以尝试使用命令行工具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:如果您收到错误“python 没有替代品”,请使用以下命令自行设置替代品:

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

Change the path /usr/bin/python3 to your desired python version accordingly.相应地将路径/usr/bin/python3更改为所需的 python 版本。

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.在我们的例子中,我们为/usr/bin/python3.6.8设置了优先级 2,因此/usr/bin/python3.6.8被 update-alternatives 命令自动设置为默认 python 版本。

we can anytime switch between the above listed python alternative versions using below command and entering a selection number:我们可以随时使用以下命令在上面列出的 python 替代版本之间切换并输入选择编号:

update-alternatives --config python

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. python pip将在新的 shell 中准备好。

Simply remove python-is-python2 :只需删除python-is-python2

sudo apt purge python-is-python2

And install python-is-python3 :并安装python-is-python3

sudo apt install python-is-python3

It will automate the process of transition to new python3.它将自动过渡到新的 python3 的过程。 Optionally you can get rid of remaining packages later:或者,您可以稍后摆脱剩余的软件包:

sudo apt autoremove && sudo apt autoclean

Set priority for default python in Linux terminal by adding this:通过添加以下内容为 Linux 终端中的默认 python 设置优先级:

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

Here, we set python3 to have priority 10 and python2 to priority 1. This will make python3 the default python.在这里,我们将python3设置为优先级 10,将python2设置为优先级 1。这将使python3成为默认 python。 If you want Python2 as default then make a priority of python2 higher then python3如果您希望 Python2 作为默认值,则将 python2 的优先级设置为高于 python3

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

The best way in ubuntu 18.04 which will work for all users is ubuntu 18.04 中适用于所有用户的最佳方式是

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.重新启动后,系统中的任何版本的 python 3 以及 python 2.7 都将被视为默认设置。 You could be more specific by saying the following in alias if you have multiple version of python 3.如果您有多个版本的 python 3,您可以通过在别名中说以下内容来更具体。

sudo vim /etc/bash.bashrc
add lines
alias python=python3.6
alias pip=pip3.6
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

You didn't include the priority argument您没有包含优先级参数

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

You can replace 5 with any priority you want.您可以将 5 替换为您想要的任何优先级。 A higher priority alternative takes precedence over lower priority.较高优先级的备选方案优先于较低优先级。

If there is a possibility to use particular python version directly, I would go for it compared to update-alternatives and alias solution.如果有可能直接使用特定的 python 版本,与update-alternativesalias解决方案相比,我会选择它。

Ex.前任。

python3.6 -m pip install pytest
ptyhon3.6 -m pytest test_sample.py

-m executes particular module for that particular python version. -m为该特定 python 版本执行特定模块。 The first line will install pytest for for that particular version and user in possible location /home/user/.local/lib/python3.5/site-packages第一行将为该特定版本和用户在可能的位置/home/user/.local/lib/python3.5/site-packages安装 pytest

在我的情况下,它发生在我在终端“ alias python=python3 ”中运行此命令时

At first, Make sure Python3 is installed on your computer首先,确保您的计算机上安装了 Python3

Go to your terminal and type:转到您的终端并输入:

cd ~/ to go to your home directory cd ~/进入你的主目录

If you didn't set up your .bash_profile yet, type touch .bash_profile to create your .bash_profile.如果您尚未设置.bash_profile ,请键入touch .bash_profile以创建您的 .bash_profile。

Or, type open -e .bash_profile to edit the file.或者,键入open -e .bash_profile来编辑文件。

Copy and save alias python=python3 in the .bash_profile.在 .bash_profile 中复制并保存alias python=python3

Close and reopen your Terminal.关闭并重新打开您的终端。 Then type the following command to check if Python3 is your default version now:然后键入以下命令来检查Python3现在是否是您的默认版本:

python --version

You should see python 3.xy is your default version.您应该看到python 3.xy是您的默认版本。

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM