如何检查 OS X 和 CentOS 上安装的所有 Python 版本

问题描述 投票:0回答:15

我今天刚开始设置 CentOS 服务器,注意到 CentOS 上的默认 Python 版本设置为 2.6.6。我想改用Python 2.7。我google了一下,发现2.6.6被YUM等系统工具使用,所以我不应该篡改它。然后我在 Mac 上打开一个终端,发现我安装了 Python 2.6.8、2.7.5 和 3.3.3。

抱歉,故事很长。简而言之,我只是想知道如何查找 CentOS 上安装的所有 Python 版本,这样我就不会意外安装两次。

python linux macos centos version
15个回答
161
投票

更简单的方法是执行下一个命令:

ls -ls /usr/bin/python*

输出如下所示:

/usr/bin/python           /usr/bin/python2.7        /usr/bin/pythonw
/usr/bin/python-config    /usr/bin/python2.7-config /usr/bin/pythonw2.7

30
投票

我们可以直接使用它来查看当前用户和 root 用户安装的所有 python,如下所示:

whereis python


17
投票

这是一种更简洁的方式来显示它们(技术上没有符号链接)。这包括 python2 和 python3 安装:

ls -1 /usr/bin/python* | grep '.*[2-3]\(.[0-9]\+\)\?$'

其中

grep
过滤末尾具有该数字模式 ($) 的 ls 的输出。

或使用

find
:

find /usr/bin/python* ! -type l

其中显示了符号链接类型 (

!
) 的所有不同 (
-type l
)。


15
投票

通过发出命令找出安装的Python版本 蟒蛇——版本: $ python --版本 Python 2.7.10

如果您看到类似的内容,则说明 Python 2.7 是您的默认版本。您还可以查看是否安装了 Python 3:

$ python3 --version
Python 3.7.2

如果你还想知道它的安装路径,你可以使用 python 和 python3 发出命令“which”:

$ which python
/usr/bin/python

$ which python3
/usr/local/bin/python3

7
投票

使用

yum list displayed
命令查找您安装的软件包。


7
投票

命令:

python --version && python3 --version

输出:

Python 2.7.10
Python 3.7.1

别名命令:

pyver

输出:

Python 2.7.10
Python 3.7.1

您可以在 .bashrc 文件中创建一个像“pyver”这样的别名,或者使用像 AText 这样的文本加速器。


4
投票

正如有人在评论中提到的,如果 CentOS 支持,您可以使用

which python
。另一个可行的命令是
whereis python
。如果这些都不起作用,您可以启动 Python 解释器,它会显示版本,或者您可以在
/usr/bin
中查找 Python 文件(python、python3 等)。


4
投票
compgen -c python | grep -P '^python\d'

这也列出了一些其他的 python 东西,但是嘿,你可以识别其中的所有 python 版本。


3
投票

这取决于您的 python 设置的默认版本。您可以通过Python版本查询:

python3 --version //to check which version of python3 is installed on your computer
python2 --version // to check which version of python2 is installed on your computer
python --version // it shows your default Python installed version.

2
投票

要查找安装的 python 版本,请使用

whereis
命令。

$ whereis python | tr ' ' '\n' | grep ^/ | sort

这不仅会显示

/usr
目录中安装的系统版本,还会显示
pyenv
工具安装的版本。

/etc/python2.7
/etc/python3.10
/etc/python3.8
/etc/python3.9
/home/user/.pyenv/shims/python
/home/user/.pyenv/shims/python3.11
/home/user/.pyenv/shims/python3.11-config
/home/user/.pyenv/shims/python3.9
/usr/bin/python
/usr/bin/python2.7
/usr/bin/python3.10
/usr/bin/python3.10-config
/usr/bin/python3.8
/usr/bin/python3.8-config
/usr/bin/python3.9
/usr/bin/python3.9-config
/usr/include/python3.10
/usr/include/python3.8
/usr/include/python3.9
/usr/lib/python2.7
/usr/lib/python3.10
/usr/lib/python3.8
/usr/lib/python3.9
/usr/local/bin/python3.6
/usr/local/bin/python3.6m
/usr/local/bin/python3.6m-config
/usr/local/lib/python2.7
/usr/local/lib/python3.10
/usr/local/lib/python3.6
/usr/local/lib/python3.8
/usr/local/lib/python3.9
/usr/share/info/python3.9
/usr/share/python

1
投票

筛选此脚本的输出。

sudo find / -name 'python*' -type f  -exec du -h {}  + | sort -r -h ~/Documents/python_locations.txt

1
投票
ls -l /usr/bin/python* & ls -l /usr/local/bin/python*

1
投票

我会添加@nurealam siddiq 答案,

python --version // it shows your default Python installed version.

python2 --version // to check which version of python2 is installed 

python3 --version //to check which version of python3 is installed 

python3.X --version // to further check which python3.X is installed


0
投票

要检查操作系统中安装的 python 版本,您可以运行以下命令:-

python2 -version
python3 -version

0
投票

在终端上输入此内容

py -0p

© www.soinside.com 2019 - 2024. All rights reserved.