检查是否安装了 python 3 时出现问题

problem in checking whether python 3 is installed

我想检查 python 3 是否安装在我的 Ubuntu 上。我正在使用这个脚本:

#!/usr/bin/env sh

#install firefox if does not exist
if ! command -v python3 >/dev/null 2>&1
        then
                echo "not installed"
        else
                echo "installed"
fi

当我 运行 这个脚本时,输出是 installed,但是当我使用 python --version 检查我的 python 时,我得到这个输出:

Python 2.7.17

据我所知,这意味着我 Ubuntu 上的 python 的最新版本是 2.7 而不是 3.x。怎么了?

command -v python3; echo $?的输出:

/usr/bin/python3
0

ls -l /usr/bin/python3的输出:

lrwxrwxrwx 1 root root 9 Nov 14 09:13 /usr/bin/python3 -> python3.7

ls -l /usr/bin/python3.7的输出:

-rwxr-xr-x 2 root root 5102632 Oct 27 11:43 /usr/bin/python3.7

which python的输出:

/usr/bin/python

ls -l /usr/bin/python的输出:

lrwxrwxrwx 1 root root 7 Jan 19 08:04 /usr/bin/python -> python2

ls -l /usr/bin/python2的输出:

lrwxrwxrwx 1 root root 9 Jan 19 08:04 /usr/bin/python2 -> python2.7

另外,我在单独的 VM 上还有另一个 Ubuntu,python --version、returns command 'python' not found 的输出,但是当我为此 VM 执行上述命令时嗯,它 returns 类似的响应(表明 python 已安装)。

尝试 whereis 命令。它会告诉你 python3 在哪里。

whereis python
或者更好
whereis python3

输出应为路径或路径列表。您可能会看到类似以下内容:

python3: /usr/bin/python3.6m /usr/bin/python3.6 /usr/bin/python3.6m-config
/usr/bin/python3 /usr/bin/python3.6-config /usr/lib/python3.6 /usr/lib/python3
/usr/lib/python3.7 /usr/lib/python3.8 /etc/python3.6 /etc/python3 /usr/local

然后确保您的 PATH 变量至少包含上述目录之一。 PATH 环境变量本质上是包含可执行文件(程序)的目录列表。当您在命令行上键入内容时,您的终端程序将在 PATH 中列出的目录中搜索您指定的可执行文件。

echo $PATH

/home/USER/.pyenv/shims:/home/USER/.pyenv/bin:/home/USER/esp/xtensa-esp32-
elf/bin:/home/USER/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

您的 PATH 中可能有一个目录出现问题,或者您可能需要添加一个新目录。

https://www.python.org/dev/peps/pep-0394/

注意:您可以拥有一个同时安装了 Python 2 和 Python 3 的环境,并且 python 可以指向任何一个。 (很可能 python2 是为了向后兼容。)Python 3 应用程序应始终使用 python3 命令而不是 python

因为许多程序都是使用 Python 2 编写的,所以许多操作系统都在其存储库中保留 Python 2,并且这个 isn't going to change 很快就会出现。

所以当你安装Python时,它添加了Python 2到/usr/bin/(可能是/usr/bin/python2,可能是/usr/bin /python2.7 等),并将 /usr/bin/python 指向同一位置。当你安装了Python3的时候,它安装了Python3,到/usr/bin/python3.

当您测试是否安装了python3 时,您发现它是。根据 PEP 394, /usr/bin/python should refer to Python 2. Ubuntu documentation 解释这意味着什么和不意味着什么:

What this does not mean:

/usr/bin/python will point to Python 3. No, this is not going to happen (unless PEP 394 advocates otherwise, which is doubtful for the foreseeable future). /usr/bin/python and /usr/bin/python2 will point to Python 2.7 and /usr/bin/python3 will point to the latest supported Python 3 version.

Python 2 will be removed from the archive. No, this is not going to happen. We expect Python 2.7 to remain supported and available in Ubuntu for quite a long time, given that PEP 373 promises upstream bug fix maintenance support until 2020. It would be nice if we could demote Python 2 to universe, but that's currently problematic for technical reasons relating to multi-Python version support in Debian/Ubuntu.

基本上,虽然所有开发都应面向 Python 3,但 python 命令(/usr/bin/python)应指向 Python 2 为了防止当前程序中断

如果您想访问Python3,建议您拨打python3。 (您也可以重新绑定 /usr/bin/python 以指向 python3,但这是非常不推荐的。对大多数用户来说更有用的解决方案是 create an alias to python3。)


简短版本:您的脚本有效。 Python 3 已安装。如果你希望终端打开 Python 3 当你输入 python, add an alias alias python=python3.