如何在 Linux、Mac 和 Windows 上检查 Python 版本

如何检查 Python 版本

以下是使用脚本检查 Python 版本的方法

Check Python Version

命令 Where 什么 输出
python –version
python -v
python -vv
命令行或终端 Windows/Mac/Linux Python 3.10.7
导入 sys
sys. version
脚本 用作字符串 ‘3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]’
导入 platform
Platform.python_version()
脚本 短格式字符串 ‘3.10.7’
Platform.python_version_tuple() 脚本 短格式元组 (‘3’,’10’,’7’)

如何使用脚本检查 Python 版本?

Python 脚本可以识别计算机上安装的 Python 版本。它使您能够验证系统中是否安装了多个版本。用于检查 Python 版本的脚本对于 Windows 操作系统、Mac OS 和 Linux 发行版都是相同的。您可以通过导入 Python 的 platform 模块或 sys 模块来创建脚本。

如下所示,可以使用以下代码来处理 sys 模块

命令行代码

import sys
sys. version

输出

'3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]'

请参阅以下命令行截图

Check the Python Version using a Script

要执行上述脚本,请先在命令行中调用 python 以获得上述结果。

如下所示,可以使用以下代码来处理 sys 模块

命令行代码

import platform
print(platform.python_version())

输出

3.10.7

请参阅以下命令行截图

Check the Python Version using a Script

在基于 Web 的解释器中使用以下代码,如下所示:–

Python 代码

from platform import python_version

print("Current Python Version-", python_version())

输出

Current Python Version- 3.8.10

请参阅以下基于 Web 的解释器截图

Check the Python Version using a Script

Check the Python Version using a Script

要获取有关 Python 版本的信息,请使用以下脚本,如下所示

Python 命令

print(sys.version_info)

输出

Sys.version_info(major=3, minor=10,micro=7,releaselevel=’final’,serial=0)

截图

Check the Python Version using a Script

要获取有关 Python 版本的信息元组,请使用以下脚本,如下所示:–

Python 命令

print(type(sys.version_info))

输出

<class ‘sys.version_info’>

截图

Check the Python Version using a Script

要获取有关 Python 版本的主要信息,请使用以下脚本,如下所示:–

Python 命令

print(sys.version_info[0])

输出

3

截图

Check the Python Version using a Script

Python 命令

print(sys.version_info.major)

输出

3

截图

Check the Python Version using a Script

要获取有关平台 Python 的信息,请使用以下脚本,如下所示:–

Python 命令

Import platform
print(platform.python_version())

输出

3.10.7

截图

Check the Python Version using a Script

要获取有关 Python 版本平台类型的信息,请使用以下脚本,如下所示:–

Python 命令

print(type(platform.python_version()))

输出

<class ‘str’>

截图

Check the Python Version using a Script

要获取有关 Python 版本的信息元组,请使用以下脚本,如下所示:–

Python 命令

print(platform.python_version_tuple())

输出

(‘3’,’7’,’0’)

截图

Check the Python Version using a Script

Python 命令

print(type(platform.python_version_tuple())

输出

<class ‘tuple’>

截图

Check the Python Version using a Script

如何使用 Windows 命令行检查 Python 版本?

以下是使用 Windows 命令行检查 Python 版本的方法

第 1 步) 通过键入 cmd 打开命令提示符

第 2 步) 当命令提示符打开时,在 CLI 中键入以下命令以获取 Python 的版本名称。

命令代码

python3 --version

输出

Python 3.10.7

注意: 在运行命令之前,请确保计算机上已预装 Python。

或者,打开命令行并键入以下命令

命令代码

python --version

输出

Python 3.10.7

请参阅下面的屏幕截图,了解在命令提示符中执行的结果

截图

Check the Python Version using Windows Command Line

如何使用 Windows PowerShell 检查 Python 版本?

检查 Windows PowerShell 中的 Python 版本

  • 通过在开始菜单中键入 PowerShell 并单击 Windows PowerShell 来打开 PowerShell 提示符。
  • 用户必须在 PowerShell 中键入以下命令以获取 Python 的版本名称。
  • Check the Python Version using Windows Command Line

    但是,在运行此命令之前,请确保计算机上已预装 Python。

    以下是在命令行中键入并执行的命令

    PowerShell 代码

    python3 --version
    

    输出

    Python 3.10.7
    

    或者,打开命令行并键入以下命令

    PowerShell 代码

    python --version
    

    输出

    Python 3.10.7
    

    请参阅此屏幕截图,了解在命令提示符中执行的结果

    Check the Python Version using Windows Command Line

    如何在 Linux 中检查 Python 版本?

    以下是检查 Linux 中 Python 版本的方法

    第 1 步)要在 Linux 中检查 Python 版本,请打开终端

    第 2 步)这将打开如下图所示的窗口

    Check the Python Version using Windows Command Line

    第 3 步)在 Linux 的终端中键入以下命令以获取 Python 的版本名称。

    以下是在终端中键入并执行的命令

    Linux 终端代码

    python3 --version
    

    输出

    Python 3.5.2
    

    或者,打开终端并键入以下命令

    Linux 终端代码

    python --version
    

    输出

    Python 2.7.12
    

    上述命令帮助用户检查系统中是否安装了 python 2。请参阅下面的屏幕截图,了解在 Linux 终端中执行的结果

    截图

    Check the Python Version using Windows Command Line

    请确保计算机的操作系统已预装 Python。大多数高级 Linux 发行版,如 Ubuntu 和 Fedora,都预装了 Python。

    或者,可以在 Linux 终端中使用以下命令来获取发行版中安装的 Python 的版本名称,如下所示:–

    Linux 终端代码

    python -VV
    

    输出

    Python 2.7.12
    

    截图:–

    Linux 终端代码

    Python3 -c “import sys; print(sys. version).”
    

    输出

    3.5.2(default, Nov 12 2018, 13:43:14)
    
    [GCC 5.4.0 20160609]
    

    截图

    Check the Python Version using Windows Command Line

    Linux 终端代码

    Python3 -c “import sys; print(sys.version_info)”
    

    输出

    sys.version_info(major=3,minor=2,releaselevel=’final’,serial=0)
    

    截图

    Check the Python Version using Windows Command Line

    Linux 终端代码

    Python3 -c “import platform; print(platform.python_version())”
    

    输出

    3.5.2
    

    截图

    Check the Python Version using Windows Command Line

    上述命令的唯一区别在于使用了 Python 模块。此模块为用于操作 Python 环境不同部分中使用的变量和函数提供了名称。该模块获取底层平台数据,如操作系统、硬件和解释器。

    如何在 Mac 上检查 Python 版本?

    以下是检查 Python 版本的方法

    第 1 步)要在 Mac OS 中检查 Python 版本

    通过在 Mac OS 桌面菜单的右上方搜索框中键入 terminal 命令来打开终端,然后单击它。

    第 2 步)这将打开如下图所示的窗口

    Check the Python Version using Windows Command Line

    第 3 步)要获取 Python 的版本名称,您需要在终端窗口中使用以下命令。

    注意:但是,在运行命令之前,请确保计算机上已预装 Python。大多数高级 Mac OS 发行版都已预装 Python。

    以下是在终端中键入并执行的命令

    Mac OS 终端代码

    python3 --version
    

    输出

    Python 3.3.1
    

    或者,打开终端并键入以下命令

    Mac OS 终端代码

    python --version
    

    输出

    Python 2.7.4
    

    上述命令帮助用户检查系统中是否安装了 python 2。

    请参阅下面的屏幕截图,了解在 Mac OS 终端中执行的结果

    截图

    Check the Python Version using Windows Command Line

    如何在同一系统中检查安装的多个 Python 版本?

    计算机系统中可能安装了多个 Python 版本。因此,一台计算机可能同时安装了版本 2 和版本 3。

    要验证两个版本,用户可以打开命令行并键入以下命令

    场景 1:检查 Python 版本 2

    命令行 \ 终端代码

    python --version
    

    场景 2:检查 Python 版本 3

    OS 命令

    python3 --version
    

    Python 2 或 Python 3

    以下是两个版本的比较

    • Python 3 与 Python 2 相比,在语法方面有所不同。这是 Python 2 和 Python 3 之间的 完整比较
    • Python 3 包含一些有助于将用 Python 2 编写的代码转换为 Python 3 的实用程序。
    • Python 3 的语法与 Python 2 不同,因为 Python 2 和 Python 3 的语法不同。
    • Python 2 不再发布任何新更新,这使得该版本与版本 3 不兼容。
    • Python 3 的一些功能已向后移植到 Python 2,这是为了支持轻松迁移。
    • Python 3 默认以 Unicode 格式存储字符串。但是,要在版本 2 中将字符串转换为 Unicode 格式,必须使用关键字“u”来定义。

    结论

    • Python 版本 2 和 Python 版本 3 的语法不同
    • 可以通过多种方式检查 Python 版本,例如
    • 使用 Windows PowerShell
    • 在 Windows 命令行中。
    • Mac OS 和 Linux 的终端
    • 使用 Python 脚本。
    • 可以通过键入 Python –version 命令来检查 Python 版本。
    • 要验证 Python 3 版本的安装,请在各自操作系统的命令行中键入 python 3 –version 命令。

    常见问题

编码器可以执行一个命令来确定您的计算机操作系统中安装了哪个版本。您可以通过打开操作系统的命令行并键入以下命令来完成此操作,如下所示

  • Linux: Ctrl + Alt + T 或 Ctrl + Alt + F2
  • Windows: Win + R
  • Mac OS: Finder > Applications > Utilities > Terminal

以下是检查版本前重要的系统必备条件

  • 用户应可以访问命令行。
  • 他们应该提前 安装最新版本的 Python
  • 建议也安装 Python 3 的任何最新次要版本。1
  • 为了验证已安装的版本,用户可以在安装后打开命令行。在某些操作系统(如 Linux 和 Mac OS)中,它也被称为终端。