修复 Ubuntu Linux 中 “Command ‘python’ not found” 的错误
Python
![](/data/attachment/album/202207/30/071627r176w1k1y5dkkw6w.jpg)


如何在 Linux 终端中运行一个 Python 程序？像这样，对吗？



```
python program.py

```

然而，如果你试图在 Ubuntu（和其他一些发行版）中使用 `python` 命令，它会抛出一个错误。



```
command ‘python’ not found, did you mean:
command ‘python3’ from deb python3
command ‘python’ from deb python-is-python3

```

如果你注意这个错误信息，它说明了很多东西。**这里的 `python` 命令实际上是 `python3`**。


如果你不理解，不用担心。我将在这里详细解释。


### 为什么在 Ubuntu 上没有发现 python 命令？


这是因为 Python 语言不是以 `python` 的形式安装的，而是以 `python3` 或 `python2` 的形式安装的（在一些老的 Ubuntu 版本中）。


在遥远的过去的某个时间点，Python 实际上是作为 `python` 包/可执行文件提供的。当 Python 发布第二版时，Ubuntu 和其他发行版不得不同时支持 Python 1.x 和 2.x 版本。


因此，他们将较新的 Python 版本命名为 `python2`，以区分这两个版本。其他应用或库也在其代码中指定 `python` 或 `python2`。


最终，Python 1 版本被完全停用，但软件包继续被命名为 `python2`。


类似地，当 Python 3 版本发布时，发行版开始同时提供 `python2` 和 `python3` 包。


Python 2 不再被支持，Python 3.x 是你在 Ubuntu 上安装的版本。该软件包仍被命名为 `python3`。


**总结一下，你已经在 Ubuntu 上安装了 Python。它是以 `python3` 软件包方式使用的。**


那么，当你 [在 Ubuntu 上看到 “Python command not found” 的错误](https://itsfoss.com/bash-command-not-found/) 时，你有什么选择？让我来介绍一下。


### 确保你的系统中已经安装了 Python


它应该已经安装了，但仔细检查一下也无妨。


Ubuntu 18.04 也有 Python 2，但 20.04 及更高版本只有 Python 3。不过，你有哪个版本：



```
type python python2 python3

```

正如你在下面的截图中看到的，我的系统上安装了 Python 3 版本。


![Checking Python version in Ubuntu](/data/attachment/album/202207/30/071649t3b6wbzcavyk267t.png)


如果你没有安装任何 Python 版本，你可以用以下命令安装 Python 3 版本。



```
sudo apt install python3

```

### 使用 python3 而不是 python


如果对你来说不是太麻烦，在需要的地方使用 `python3` 命令而不是 `python`。


想检查已安装的 Python 版本吗？请这样输入：



```
python3 --version

```

然后你会在输出中得到版本的详细信息：



```
~$ python3 --version
Python 3.10.4

```

如果你必须运行一个 Python 程序，请像这样执行它：



```
python3 program.py

```

这在大多数情况下应该对你有用。但是，如果你使用的是一些（旧的）Python 应用，期望在其代码中运行 Python 可执行文件，你就会有问题。别担心，你也可以绕过它。


### 将 python3 链接为 python


你可以在你的 `.bashrc` 文件中创建一个永久别名，像这样：



```
alias python='python3'

```

这样，你可以运行 `python` 命令，而你的系统运行 `python3`。


这在大多数情况下都会起作用，除非某些程序期望运行 `/usr/bin/python`。现在，你可以在 `/usr/bin/python` 和 `/usr/bin/python3` 之间建立符号链接，但对于 Ubuntu 用户来说，存在一个更简单的选择。


对于 Ubuntu 20.04 和更高版本，如果你安装了 `python-is-python3` 软件包，你有一个软件包可以自动完成所有链接创建。这也是原始错误信息所提示的。



```
sudo apt install python-is-python3

```

![install python is python3 ubuntu](/data/attachment/album/202207/30/071649fitwdowttttdhutu.png)


你可以看到符号链接已经被创建，你可以使用 `python` 命令（实际上是运行 `python3`），没有任何问题。


![checking python ubuntu](/data/attachment/album/202207/30/071650elcug2c4olqolalp.png)


我希望这能澄清 Ubuntu 中 Python 软件包的问题。如果你有任何问题或建议，请告诉我。




---


via: <https://itsfoss.com/python-not-found-ubuntu/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lkxed](https://github.com/lkxed) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

How do you run a Python program in the Linux terminal? Like this, right?

`python program.py`


However, if you try to use the `python`

command in Ubuntu (and some other distributions), it will throw an error.

Command ‘python’ not found, did you mean:

command ‘python3’ from deb python3

command ‘python’ from deb python-is-python3

If you pay attention to the error message, it clears a lot of things. *The python command is actually python3 here.*

If you don’t understand it, no worries. I’ll explain things in detail here.

## Why there is no python command found on Ubuntu?

It’s because the Python language is not installed as python but python3 or python2 (in some older Ubuntu versions).

At some point in time in the distant past, Python was actually available as `python`

package/executable. When Python released version 2, Ubuntu and other distros had to provide support for both Python version 1.x and 2.x.

So, they named the newer Python version `python2`

to distinguish between the two. Other applications or libraries also specified python or python2 in their code.

Eventually, Python version 1 was discontinued completely but the package continued to be named python2.

Similarly, when Python version 3 was released, distributions started providing both `python2`

and `python3`

packages.

Python 2 is no longer supported and Python 3.x is what you get on Ubuntu. The package is still named python3.

*To summarize, you have Python installed on Ubuntu already. It is available as python3 package.*

So, what are your options when you see Python [command not found error on Ubuntu](https://itsfoss.com/bash-command-not-found/)? Let me go over them.

## Make sure you have Python installed on your system

It should already be installed but no harm in double checking.

Ubuntu 18.04 had Python 2 as well but 20.04 and higher versions have Python 3 only. Still, which version(s) you have with:

`type python python2 python3`


As you can see in the screenshot below, I have Python version 3 installed on my system.

![check python version ubuntu](https://itsfoss.com/content/images/wordpress/2022/07/check-python-version-ubuntu.png)

If you don’t have any Python version installed, you may install Python version 3 with the following command:

`sudo apt install python3`


## Use python3 instead of python

If it’s not too much of a trouble for you, use python3 command instead of python wherever required.

Want to check the installed python version? Use it like this:

`python3 --version`


And you get the version details in the output:

```
abhishek@itsfoss:~$ python3 --version
Python 3.10.4
```


If you have to run a Python program, execute it like this:

`python3 program.py`


This should work for you in most cases. However, if you are using some (old) Python application that expects to run the python executable in its code, you’ll have issues. Don’t worry, you can get around it as well.

## Link python3 as python

You can create a permanent alias in your .bashrc file like this:

`alias python='python3'`


This way, you can run the `python`

command and your system runs `python3`

.

It will work in most cases unless some program expects to run /usr/bin/python. Now, you may create symlink between /usr/bin/python and /usr/bin/python3 but there exists a simpler option for Ubuntu users.

For Ubuntu 20.04 and higher versions, you have a package that does all link creation automatically if you install the python-is-python3 package. This is what the original error message has also suggested.

`sudo apt install python-is-python3`


![install python is python3 ubuntu](https://itsfoss.com/content/images/wordpress/2022/07/install-python-is-python3-ubuntu.png)

You can see that symlinks have been created and you can use the python command (which actually runs python3) without any issues.

![checking python ubuntu](https://itsfoss.com/content/images/wordpress/2022/07/checking-python-ubuntu.png)

I hope this clears the air on the Python package in Ubuntu. Let me know if you have any questions or suggestions.