首页 > 编程知识 正文

Python platform模块

时间:2023-11-26 13:13:22 阅读:309434 作者:GZJQ

一个简单的事实是,我们的设备或系统所在的平台是系统性能的关键因素。我们指的是系统的操作系统(OS),我们在其上执行某些操作的应用版本,等等。,作为性能分析的平台。了解平台非常重要,因为它有助于我们了解我们希望在系统中使用的应用或我们希望执行的功能是否与我们正在使用的操作系统应用兼容。当我们在别人的系统上工作时,平台信息也是一个非常重要的因素。但是,如果我们要手动检查关于平台的所有这些信息,这将需要一段时间,并且需要我们付出很多努力。当我们想要用一些新的功能来执行操作或者在新的应用或软件上工作时,它会变得更加忙碌。因此,我们不能一直手动检查平台信息,我们需要其他东西来执行这个任务,这是 Python 提供给我们的。

Python 为我们提供了 Platform 模块,该模块用于检索我们正在运行的平台的所有信息。我们只需执行一个 Python 程序就可以获取所有这些信息。因此,我们将在本教程中了解 Python 的platform模块,并了解如何实现和使用该模块从我们的系统中获取信息。

Python platform模块介绍

platform模块是一个 Python 包,可以检索关于我们运行 Python 的平台的所有信息。通过在 Python 程序中使用该模块的功能并执行该程序,我们可以获取关于运行 Python 的系统的所有信息。Python 的platform模块可以从系统中获取以下信息:

  • Python 所在的操作系统,
  • 运行程序的操作系统版本,
  • Python 版本,
  • 机器或系统的类型,
  • 节点版本等。

每当我们在一个我们不知道信息的系统上工作时,platform模块对我们非常有帮助,我们想知道关于它的这些细节。每当我们必须在系统中安装几个软件和应用时,这个模块都起着至关重要的作用,我们希望检查我们的系统是否与该应用兼容。在安装任何应用之前,我们可以通过检查系统的版本、机器类型和其他几个属性来检查系统的兼容性。如果我们想更实际地了解 Python platform模块的实现,那么请看下面的示例场景:

示例:

假设我们必须为 Python 安装一个外部库来执行一些特定的操作,或者我们必须为 Python 安装一个扩展,那么我们首先要确保我们的系统和 Python 版本与外部库或扩展兼容。在这里,我们可以使用 Python 的platform模块从系统中了解所有这些属性,这就是这个模块在 Python 中发挥关键作用的方式。

Python 的platform模块:安装

platform模块是 Python 的内置模块(这意味着它与其他内置 Python 包一起提供),这就是为什么我们不必为该模块执行任何安装过程。我们可以使用以下代码行在 Python 程序中导入和使用platform模块的功能:


import platform

Python 的platform模块:实现

platform模块具有多种功能,用于获取有关系统多种属性的信息,如机器类型、系统名称、系统版本、处理器信息等。我们将在实现部分使用其中的一些,并了解如何使用platform模块检索信息。我们将为platform模块的每个功能使用一个示例程序,并在输出中打印数据。

下面是platform模块及其功能的几个例子和实现:

应用 1:显示系统处理器:

我们可以使用platform模块的处理器()功能显示系统的处理器。通过在一个示例程序中使用处理器()函数,我们可以了解它的工作原理。

例 1:

请看下面的 Python 程序,我们在输出中打印了处理器信息:


# Importing platform module in the program
import platform as plt
# Fetching processor information
processorInformation = plt.processor()
# Printing results
print("The processor of the system in which we are running the program is: ", processorInformation)

输出:

The processor of the system in which we are running the program is:  Intel64 Family 6 Model 61 Stepping 4, GenuineIntel

如我们所见,处理器名称及其详细信息会打印在输出中,并且该信息会根据我们运行此示例的系统而有所不同。

应用 2:展示平台架构:

platform模块为我们提供了 architecture()函数,我们可以用它来了解系统的架构,我们甚至可以在输出中打印这些信息。我们可以通过在一个示例程序中使用它来理解 architecture()函数的工作原理。

例 2:

请看下面的 Python 程序,我们在输出中打印了系统的架构信息:


# Importing platform module in the program
import platform as plt
# Fetching architecture of the system
archInfo = plt.architecture()
# Printing results
print("The architecture of the system in which we are running the program is: ", archInfo)

输出:

The architecture of the system in which we are running the program is:  ('64bit', 'WindowsPE')

正如我们所看到的,系统的架构细节被打印在输出中,并且这个信息可以根据我们运行这个例子的系统而变化。

应用 3:机器的使用()功能:

如果我们想打印我们正在工作的机器类型,我们可以使用platform模块的 machine()功能。函数的作用是:在输出中返回一个字符串,显示执行程序的机器类型。这里,我们称机器类型为我们正在处理的系统核心中可用的寄存器宽度的大小。通过在一个示例程序中使用 machine()函数,我们可以理解它的工作原理。

例 3:

请看下面的 Python 程序,我们在其中使用 machine()函数打印了机器类型:


# Importing platform module in the program
import platform as plt
# Fetching machine type from the system
machInfo = plt.machine()
# Printing results
print("The type of machine on which we are running the program is: ", machInfo)

输出:

The type of machine on which we are running the program is:  AMD64

正如我们所看到的,关于机器类型系统的细节被打印在输出中,这可以根据系统而变化。

应用 4:获取网络名称:

平台为我们提供了 node()函数,它获取系统节点的信息。platform模块的 Node()函数返回一串显示系统网络名称的结果,也可以说是节点信息。通过在一个示例程序中使用 node()函数,我们可以理解它的工作原理。

例 4:

请看下面的 Python 程序,我们在其中使用 node()函数打印了网络类型的系统:


# Importing platform module in the program
import platform as plt
# Fetching node type from the system
nodeInfo = plt.node()
# Printing results
print("The system's network name or node details of system on which we are running the program is: ", nodeInfo)

输出:

The system's network name or node details of the system on which we are running the program is:  Manish-Arora-Laptop

正如我们所看到的,系统的网络名称或模式会打印在输出中,并且这些信息会根据它所连接的系统和网络而有所不同。

应用 5:操作系统或系统平台名称:

平台系统和操作系统(OS)非常相似,当我们打印平台信息时,它也会显示操作系统的名称。因此,如果我们想只知道操作系统的名称,那么我们将使用platform模块的 System()函数,如果我们想获得关于平台的完整信息,那么我们将使用 platform()函数。在这里,我们将通过在一个示例程序中使用 system()和 platform()函数来理解它们的实现。

例 5:

看看下面的 Python 程序,其中我们使用了 system()和 platform()函数从系统中获取信息:


# Importing platform module in the program
import platform as plt
# Fetching name of operating system
osInfo = plt.system()
# Fetching information of the platform
platformInfo = plt.platform()
# Printing results
print("The system's Operating System Name on which we are running the program is: ", osInfo)
print("Platform's information on which we are running the program: ", platformInfo)

输出:

The system's Operating System Name on which we are running the program is:  Windows
Platform's information on which we are running the program:  Windows-10-10.0.19041-SP0

正如我们所看到的,输出中会打印出有关我们正在使用的操作系统的平台和名称的详细信息,这些信息可能会因不同的系统及其操作系统而异。

应用 6:了解安装的 Python 版本:

platform模块为我们提供了 python_version()函数,我们可以用它来了解我们系统中安装的 python 版本。当我们必须检查 Python 的版本以及这个版本是否与最新发布的模块兼容时,这个功能非常有帮助。我们可以通过在一个示例程序中使用 python_version()函数来理解它的工作原理。

示例 6: 看看下面的 Python 程序,我们在输出中打印安装了 Python 的版本:


# Importing platform module in the program
import platform as plt
# Fetching installed version information
pvInfo = plt.python_version()
# Printing results
print("The version of Python installed in our system is: ", pvInfo)

输出:

The version of Python installed in our system is:  3.9.0

正如我们所看到的,安装好的 Python 版本会打印在输出中,对于每个使用不同版本 Python 的人来说,它会有所不同。

应用 7:显示 Python 的分支:

从这里的 Python 分支,我们指的是 Python 的 SCM 分支,其中 SCM 代表源代码管理器。借助 Platform 模块的 python_branch()函数,我们可以很容易地知道我们的系统中安装了哪个 SCM 分支 python。配置管理或源代码管理用于跟踪软件的修订和更新。我们可以通过在一个示例程序中使用 python_branch()函数来理解它的工作原理。

例 7:

请看下面的 Python 程序,我们在输出中打印并安装了 Python 的 SCM 分支名称:


# Importing platform module in the program
import platform as plt
# Fetching installed Python's SCM branch name
pbInfo = plt.python_branch()
# Printing results
print("The name of SCM branch of Python which is installed in our system is: ", pbInfo)

输出:

The name of SCM branch of Python which is installed in our system is:  tags/v3.9.0

我们可以看到,安装后的 Python 的 SCM 分支名称会打印在输出中,对于每个使用不同 Python 分支版本的人来说,它会有所不同。

应用 Python 编译器信息:

platform模块为我们提供了 python_compiler()函数,我们可以用它来获取系统中存在的 python 编译器的完整信息。函数的作用是:返回一个结果字符串,显示用于编译所有 python 程序的 Python 编译器的信息。我们可以通过在一个示例程序中使用 python_compiler()函数来理解它的工作原理。

例 8:

看看下面的 Python 程序,我们在输出中打印了关于 Python 编译器的信息:


# Importing platform module in the program
import platform as plt
# Fetching installed Python's compiler information
pcInfo = plt.python_compiler()
# Printing results
print("Information regarding Python compiler present in our system: ", pcInfo)

输出:

Information regarding Python compiler present in our system:  MSC v.1927 64 bit (AMD64)

我们可以看到,安装好的 Python 的编译器信息会打印在输出中,对于不同的用户可能是相同的,也可能是不同的。

应用 9:显示 Python 的构建日期和时间:

我们甚至可以知道使用platform模块的系统中 Python 版本的构建日期和时间。platform模块为我们提供了 python_build()函数,该函数返回一个结果字符串,该字符串显示了系统中 python 版本的构建日期和时间信息。我们可以通过在一个示例程序中使用 python_build()函数来理解它的工作原理。

例 9:

看看下面的 Python 程序,我们在输出中打印了关于 Python 构建日期和时间的信息:


# Importing platform module in the program
import platform as plt
# Fetching information about Python's build date & time
pbInfo = plt.python_build()
# Printing results
print("Information regarding build date & time of Python version present in our system: ", pbInfo)

输出:

Information regarding build date & time of Python version present in our system: ('tags/v3.9.0:9cf6752', 'Oct  5 2020 15:34:40')

正如我们所看到的,安装的 Python 版本的构建日期和时间会打印在输出中,根据不同用户系统中的 Python 版本,它可能相同,也可能不同。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。