一個簡單的事實是,我們的設備或系統所在的平台是系統性能的關鍵因素。我們指的是系統的操作系統(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 版本,它可能相同,也可能不同。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/244436.html