platform --- 對底層平臺識別資料的存取

原始碼:Lib/platform.py


備註

特定平臺清單為按字母順序排列,Linux 包括在 Unix 小節之中。

跨平台

platform.architecture(executable=sys.executable, bits='', linkage='')

查詢給定的可執行檔案(預設為 Python 直譯器二進位制檔案)來獲取各種架構資訊。

回傳一個 tuple(元組) (bits, linkage),其中包含可執行檔案所使用的位元架構和連結格式資訊。這兩個值均以字串形式回傳。

無法確定的值將回傳參數所給定之預先設置值。如果給定的位元為 '',則會使用 sizeof(pointer)(或者當 Python 版本 < 1.5.2 時為 sizeof(long))作為所支援指標大小的指示器 (indicator)。

此函式依賴於系統的 file 命令來執行實際的操作。這在幾乎所有 Unix 平臺和某些非 Unix 平臺上,只有當可執行檔案指向 Python 直譯器時才可使用。當以上要求不滿足時將會使用合理的預設值。

備註

在 macOS(也許還有其他平臺)上,可執行檔案可能是包含多種架構的通用檔案。

要獲取當前直譯器的 "64 位元性 (64-bitness)",更可靠的做法是查詢 sys.maxsize 屬性:

is_64bits = sys.maxsize > 2**32
platform.machine()

回傳機器種類,例如 'AMD64'。如果該值無法確定則會回傳一個空字串。

platform.node()

回傳電腦的網路名稱(可能不是完整名稱!)。如果該值無法確定則會回傳一個空字串。

platform.platform(aliased=False, terse=False)

會儘可能附帶有用資訊地回傳一個標識底層平臺的字串。

輸出應為人類易讀的 (human readable),而非機器易剖析的 (machine parseable)。它在不同平臺上看起來可能不一致,這是有意為之的。

如果 aliased 為真值,此函式將使用各種不同於平臺通用名稱的別名來回報系統名稱,例如 SunOS 將被回報為 Solaris。system_alias() 函式被用於實作此功能。

terse 設為真值將導致此函式只回傳標識平臺所需的最小量資訊。

在 3.8 版的變更: 在 macOS 上,如果 mac_ver() 回傳的釋出版字串非空字串,此函式現在會使用它以獲取 macOS 版本而非 darwin 版本。

platform.processor()

回傳(真實的)處理器名稱,例如 'amdk6'

如果該值無法確定則將回傳空字串。請注意,許多平臺都不提供此資訊或是簡單地回傳與 machine() 相同的值。NetBSD 則會提供此資訊。

platform.python_build()

回傳一個 tuple (buildno, builddate),表示字串形式的 Python 建置編號和日期。

platform.python_compiler()

回傳一個標識用於編譯 Python 的編譯器的字串。

platform.python_branch()

回傳一個標識 Python 實作 SCM 分支的字串。

platform.python_implementation()

回傳一個標識 Python 實作的字串。可能的回傳值有:'CPython'、'IronPython'、'Jython'、'PyPy'。

platform.python_revision()

回傳一個標識 Python 實作 SCM 修訂版的字串。

platform.python_version()

將 Python 版本以字串 'major.minor.patchlevel' 形式回傳。

請注意此回傳值不同於 Python sys.version,它總是會包括 patchlevel(預設為 '0')。

platform.python_version_tuple()

將 Python 版本以字串 tuple (major, minor, patchlevel) 形式回傳。

請注意此回傳值不同於 Python sys.version,它總是會包括 patchlevel(預設為 '0')。

platform.release()

回傳系統的釋出版本,例如 '2.2.0''NT',如果該值無法確定則將回傳一個空字串。

platform.system()

回傳系統/OS 的名稱,例如 'Linux''Darwin''Java''Windows'。如果該值無法確定則回傳一個空字串。

On iOS and Android, this returns the user-facing OS name (i.e, 'iOS, 'iPadOS' or 'Android'). To obtain the kernel name ('Darwin' or 'Linux'), use os.uname().

platform.system_alias(system, release, version)

回傳做為某些系統所使用的常見行銷名稱之別名的 (system, release, version)。它還會在可能導致混淆的情況下對資訊進行一些重新排序。

platform.version()

回傳系統的釋出版本資訊,例如 '#3 on degas'。如果該值無法確定則將回傳一個空字串。

On iOS and Android, this is the user-facing OS version. To obtain the Darwin or Linux kernel version, use os.uname().

platform.uname()

具有高可攜性 (portable) 的 uname 介面。回傳包含六個屬性的 namedtuple()systemnodereleaseversionmachineprocessor

processor 會延遲解析,有需求時才會解析

注意:前兩個屬性名稱與 os.uname() 提供的名稱不同,它們分別命名為 sysnamenodename

無法確定的條目會被設為 ''

在 3.3 版的變更: 將結果從 tuple 改為 namedtuple()

在 3.9 版的變更: processor 會延遲解析,並非立即解析。

Java 平台

platform.java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', ''))

Jython 的版本介面。

回傳一個 tuple (release, vendor, vminfo, osinfo),其中 vminfo 為 tuple (vm_name, vm_release, vm_vendor)osinfo 為 tuple (os_name, os_version, os_arch)。無法確定的值將被設為由參數所給定的預設值(預設均為 '')。

自從版本 3.13 後不推薦使用,將會自版本 3.15 中移除。: It was largely untested, had a confusing API, and was only useful for Jython support.

Windows 平台

platform.win32_ver(release='', version='', csd='', ptype='')

從 Windows 登錄檔 (Window Registry) 獲取額外的版本資訊並回傳一個 tuple (release, version, csd, ptype),它代表 OS 發行版、版本號、CSD 級別 (service pack) 和 OS 類型(多個/單個處理器)。

一點提示:ptype 在單個處理器的 NT 機器上為 'Uniprocessor Free',而在多個處理器的機器上為 'Multiprocessor Free''Free' 是指該 OS 版本沒有除錯程式。它也可能以 'Checked' 表示,代表該 OS 版本使用了除錯程式,即檢查引數、範圍等的程式。

platform.win32_edition()

回傳一個代表當前 Windows 版本的字串。可能的值包括但不限於 'Enterprise''IoTUAP''ServerStandard''nanoserver'

Added in version 3.8.

platform.win32_is_iot()

如果 win32_edition() 回傳的 Windows 版本被識別為 IoT 版則回傳 True

Added in version 3.8.

macOS 平台

platform.mac_ver(release='', versioninfo=('', '', ''), machine='')

獲取 Mac OS 版本資訊並將其回傳為 tuple (release, versioninfo, machine),其中 versioninfo 是一個 tuple (version, dev_stage, non_release_version)

無法確定的條目會被設為 ''。所有 tuple 條目均為字串。

iOS Platform

platform.ios_ver(system='', release='', model='', is_simulator=False)

Get iOS version information and return it as a namedtuple() with the following attributes:

  • system is the OS name; either 'iOS' or 'iPadOS'.

  • release is the iOS version number as a string (e.g., '17.2').

  • model is the device model identifier; this will be a string like 'iPhone13,2' for a physical device, or 'iPhone' on a simulator.

  • is_simulator is a boolean describing if the app is running on a simulator or a physical device.

Entries which cannot be determined are set to the defaults given as parameters.

Unix 平台

platform.libc_ver(executable=sys.executable, lib='', version='', chunksize=16384)

嘗試確認可執行檔案(預設為 Python 直譯器)所連結到的 libc 版本。回傳一個字串 tuple (lib, version),當查詢失敗時其預設值將被設為給定的參數值。

請注意,此函式對於不同 libc 版本如何為可執行檔案新增符號的方式有深層的關聯,可能僅適用於以 gcc 編譯出來的可執行檔案。

檔案會以 chunksize 位元組大小的分塊 (chunk) 來讀取和掃描。

Linux 平台

platform.freedesktop_os_release()

os-release 檔案獲取作業系統標識,並將其作為一個字典回傳。os-release 檔案為 freedesktop.org 標準、並在大多數 Linux 發行版上可用。一個重要的例外是 Android 和基於 Android 的發行版。

/etc/os-release/usr/lib/os-release 均無法被讀取時將引發 OSError 或其子類別。

成功時,該函式將回傳一個字典,其中鍵和值均為字串。值當中的特殊字元例如 "$ 會被移除引號 (unquoted)。欄位 NAMEIDPRETTY_NAME 總會按照標準來定義。所有其他欄位都是可選的。根據不同廠商可能會包括額外的欄位。

請注意 NAMEVERSIONVARIANT 等欄位是適用於向使用者展示的字串。程式應當使用 IDID_LIKEVERSION_IDVARIANT_ID 等欄位來標識 Linux 發行版。

範例:

def get_like_distro():
    info = platform.freedesktop_os_release()
    ids = [info["ID"]]
    if "ID_LIKE" in info:
        # ids are space separated and ordered by precedence
        ids.extend(info["ID_LIKE"].split())
    return ids

Added in version 3.10.

Android Platform

platform.android_ver(release='', api_level=0, manufacturer='', model='', device='', is_emulator=False)

Get Android device information. Returns a namedtuple() with the following attributes. Values which cannot be determined are set to the defaults given as parameters.

  • release - Android version, as a string (e.g. "14").

  • api_level - API level of the running device, as an integer (e.g. 34 for Android 14). To get the API level which Python was built against, see sys.getandroidapilevel().

  • manufacturer - Manufacturer name.

  • model - Model name – typically the marketing name or model number.

  • device - Device name – typically the model number or a codename.

  • is_emulator - True if the device is an emulator; False if it's a physical device.

Google maintains a list of known model and device names.

Added in version 3.13.