如何检查 Python Linux、Mac 和 Windows

⚡ 智能摘要

Python version tells you which release is installed to ensure compatibility with your code and libraries. The examples below show how to check it on Windows, Linux, and Mac using the command line, PowerShell, and scripts.

  • 🔍 快速检查: Run python –version in the command line or terminal to see the installed version.
  • 📜 Script Method: Use the sys or platform module inside a Python script to read the version.
  • 🪟 Windows: Check the version from Command Prompt or PowerShell.
  • 🐧 Linux and Mac: Use the terminal with python –version or python3 –version.
  • 🔢 多个版本: You can detect several Python versions installed on the same system.
  • ⚠️ Python 2 对 3: The two versions use different syntax, so knowing the version matters.

如何检查 Python 版本

如何检查 Python 版本

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

确保 Python 版本

命令 地点 什么是 输出
python -version
python-v 命令
python-vv 命令
命令行或终端 Windows/苹果电脑/Linux Python 3.10.7
导入系统
系统版本
脚本 用作字符串 '3.10.7 (tags/v3.10.7:6cc6b13,5 年 2022 月 14 日,08:36:1933) [MSC v.64 64 位 (AMDXNUMX)]'
进口平台
平台.python_version()
脚本 短格式的字符串 “3.10.7”
平台.python_version_tuple() 脚本 短格式的元组 ('3','10','7')

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

Python 脚本 可以识别版本 Python 安装在计算机上。它使您能够验证系统中是否安装了多个版本。为检查 Python Windows OS、Mac OS 和 Linux 发行版的版本保持不变。您可以通过导入 Python 的平台模块或 sys 模块来创建脚本。

以下代码可用于 sys 模块,如下所示:

命令行 Code:

import sys
sys. version

输出:

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

参考以下命令行截图:

访问 Python 使用脚本的版本

要执行上述脚本,首先在命令行中调用python即可得到上述结果。

以下代码可用于 sys 模块,如下所示:

命令行 Code:

import platform
print(platform.python_version())

输出:

3.10.7

参考以下命令行截图:

访问 Python 使用脚本的版本

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

Python 码:

from platform import python_version

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

输出:

Current Python Version- 3.8.10

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

访问 Python 使用脚本的版本

访问 Python 使用脚本的版本

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

Python 命令:

print(sys.version_info)

输出

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

截图

访问 Python 使用脚本的版本

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

Python 命令

print(type(sys.version_info))

输出

<class ‘sys.version_info’>

截图

访问 Python 使用脚本的版本

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

Python 命令

print(sys.version_info[0])

输出

3

截图

访问 Python 使用脚本的版本

Python 命令

print(sys.version_info.major)

输出

3

截图

访问 Python 使用脚本的版本

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

Python 命令

Import platform
print(platform.python_version())

输出

3.10.7

截图

访问 Python 使用脚本的版本

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

Python 命令

print(type(platform.python_version()))

输出

<class ‘str’>

截图

访问 Python 使用脚本的版本

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

Python 命令

print(platform.python_version_tuple())

输出

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

截图

访问 Python 使用脚本的版本

Python 命令

print(type(platform.python_version_tuple())

输出

<class ‘tuple’>

截图

访问 Python 使用脚本的版本

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

以下是检查步骤 Python 版本使用 Windows 命令行:

步骤1) 按 ty 打开命令提示符ping CMD

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

命令代码:

python3 --version

输出:

Python 3.10.7

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

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

命令代码:

python --version

输出:

Python 3.10.7

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

截图:

访问 Python 使用版本 Windows 命令行

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

要检查 Python 版本中 Windows 电源外壳:

  • 按 ty 打开 PowerShell 提示符ping 在开始菜单中选择 PowerShell,然后单击 Windows 电源外壳。
  • 用户必须在 PowerShell的 获得 Python 版本名称。

访问 Python 使用版本 Windows 命令行

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

以下是在命令行中输入的命令:

PowerShell 代码:

python3 --version

输出:

Python 3.10.7

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

PowerShell 代码:

python --version

输出:

Python 3.10.7

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

访问 Python 使用版本 Windows 命令行

如何检查 Python Linux 中的版本?

以下是检查步骤 Python Linux 中的版本:

步骤1) 要检查 Python Linux 版本,打开终端

步骤2) 这将打开以下窗口,如下所示:

访问 Python 使用版本 Windows 命令行

步骤3) 在Linux终端中输入以下命令获取版本名称 Python.

以下是在终端中输入的命令:

Linux 终端代码:

python3 --version

输出:

Python 3.5.2

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

Linux终端代码:

python --version

输出:

Python 2.7.12

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

截图:

访问 Python 使用版本 Windows 命令行

确保这件事 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]

截图:

访问 Python 使用版本 Windows 命令行

Linux终端代码:

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

输出

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

截图:

访问 Python 使用版本 Windows 命令行

Linux终端代码:

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

输出

3.5.2

截图:

访问 Python 使用版本 Windows 命令行

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

如何检查 Python Mac 上有版本吗?

以下是检查步骤 Python 版本:

步骤1) 要检查 Python Mac OS 中的版本

按下 ty 键打开终端。ping 在 Mac OS 桌面菜单右上角的搜索框中输入终端命令,然后点击它。

步骤2) 这将打开以下窗口,如下所示:

访问 Python 使用版本 Windows 命令行

步骤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 终端中执行的结果请参考下面的截图:

截图:

访问 Python 使用版本 Windows 命令行

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

可以有多个 Python 版本 2 和 3 都安装在计算机系统上。因此,一台计算机可以同时安装版本 XNUMX 和 XNUMX。

为了验证两个版本,用户可以打开命令行并输入以下命令:

场景 1:检查 Python 2版

命令行\终端代码:

python --version

场景 2:检查 Python 3版

操作系统命令:

python3 --version

Python 2或 Python 3

以下是两个版本的比较:

  • Python 版本 3,相比 Python 版本 2 在语法方面有所不同。以下是 完整比较 两个版本之间。
  • Python 3 包含一些实用程序,可帮助翻译用 Python 要2 Python 3.
  • Python 版本 3 的语法与 Python 版本 2 因为 Python 版本 2 和 3 有不同的语法。
  • Python 2 没有发布任何新的更新,这使得该版本与版本 3 不兼容。
  • 中的某些功能 Python 3 被移植到 Python 2,这样做是为了支持轻松迁移。
  • Python 3 版本默认以 Unicode 格式存储字符串。但在 2 版本中,要将字符串转换为 Unicode 格式,必须使用关键字“u”进行定义。

常见问题

程序员只需执行一条命令即可识别您计算机操作系统中安装的版本。您可以通过打开操作系统的命令行并输入以下命令来完成此操作。ping 执行以下命令,如下所示:

  • Linux的: Ctrl + Alt + T 或 Ctrl + Alt + F2
  • Windows: WIN + R
  • Mac OS中: Finder>应用程序>实用程序>终端

检查版本之前,系统的重要前提条件如下:

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

You can check pip, the Python package manager, by running pip –version or pip3 –version in the command line. This prints the pip version along with the Python version it is linked to.

此 Python version determines which language features and libraries you can use. Python 2和 Python 3 have different syntax, and many packages support only specific versions, so checking the version prevents compatibility errors.

Download the latest installer from python.org and run it, or use a version manager like pyenv. On Linux you can use the package manager. Always confirm the new version with python –version afterward.

Yes. Libraries such as TensorFlow, PyTorch, and scikit-learn support only certain Python versions. Using a supported, recent Python 3 release avoids installation errors and ensures you get the latest performance and security updates.

Yes. AI assistants can read error messages, identify version mismatches, and suggest the correct Python or package versions to install. They also help set up virtual environments so different projects can use different versions safely.

总结一下这篇文章: