18.5. platform — 系统版本信息

未匹配的标注

目的:探测底层平台的硬件,操作系统和解释器版本信息。

虽然 Python 经常被用作跨平台语言,但是偶尔也有必要了解一下运行程序的是什么类型的系统。 构建工具需要该信息,但应用程序可能也知道它使用的某些库或外部命令在不同的操作系统上具有不同的接口。 例如,管理操作系统的网络配置的工具可以定义网络接口、别名、 IP 地址等的可移植表示。但是,当需要编辑配置文件时,它必须了解有关主机的更多信息,以便它可以使用正确的操作系统配置命令和文件。 platform 模块包括用于了解程序运行的解释器、操作系统和硬件平台的工具。

注意

本节中的示例输出是在三个系统上生成的:运行 OS X 10.11.6 的 Mac mini ,运行 Ubuntu Linux 14.04 的 Dell PC ,以及运行 Windows 10 的 VirtualBox VM 。 Python 安装在 OS X 和 Windows 系统上使用 来自 python.org 的预编译安装程序。 Linux 系统运行系统包中一个版本。

特别感谢 Patrick Kettner( @patrickkettner )帮助收集 Windows 上的示例输出。

解释器

platform有四个函数可以获取有关当前 Python 解释器的信息。其中包括 python_version()python_version_tuple() 返回不同形式的解释器版本,包括主要、次要和补丁级别的组件。 python_compiler() 报告用于构建解释器的编译器。 并且 python_build() 给出了解释器构建的版本字符串。

platform_python.py

import platform

print('Version      :', platform.python_version())
print('Version tuple:', platform.python_version_tuple())
print('Compiler     :', platform.python_compiler())
print('Build        :', platform.python_build())

OS X:

$ python3 platform_python.py

Version      : 3.6.4
Version tuple: ('3', '6', '4')
Compiler     : GCC 4.2.1 (Apple Inc. build 5666) (dot 3)
Build        : ('v3.6.4:d48ecebad5', 'Dec 18 2017 21:07:28')

Linux:

$ python3 platform_python.py

Version      : 3.5.2
Version tuple: ('3', '5', '2')
Compiler     : GCC 4.8.4
Build        : ('default', 'Jul 17 2016 00:00:00')

Windows:

C:\>Desktop\platform_python.py

Version      : 3.5.1
Version tuple: ('3', '5', '1')
Compiler     : MSC v.1900 64 bit (AMD64)
Build        : ('v3.5.1:37a07cee5969', 'Dec  6 2015 01:54:25')

平台

platform() 函数返回一个包含通用平台标识符的字符串。 该函数接受两个可选的布尔参数。 如果 aliased 为 True ,则返回值中的名称将从正式名称转换为更常见的名称。 如果 terse 为 True ,则返回丢弃了某些部分的最小化值而不是完整字符串。

platform_platform.py

import platform

print('Normal :', platform.platform())
print('Aliased:', platform.platform(aliased=True))
print('Terse  :', platform.platform(terse=True))

OS X:

$ python3 platform_platform.py

Normal : Darwin-17.4.0-x86_64-i386-64bit
Aliased: Darwin-17.4.0-x86_64-i386-64bit
Terse  : Darwin-17.4.0

Linux:

$ python3 platform_platform.py

Normal : Linux-3.13.0-55-generic-x86_64-with-Ubuntu-14.04-trusty
Aliased: Linux-3.13.0-55-generic-x86_64-with-Ubuntu-14.04-trusty
Terse  : Linux-3.13.0-55-generic-x86_64-with-glibc2.9

Windows:

C:\>platform_platform.py

Normal : Windows-10-10.0.10240-SP0
Aliased: Windows-10-10.0.10240-SP0
Terse  : Windows-10

操作系统和硬件信息

platform模块还可以检索有关运行解释器的操作系统和硬件的更多详细信息。 uname() 返回一个包含系统、节点、版本、版本、机器和处理器值的元组。 可以通过下表中列出的相同名称的函数访问各个值。

平台信息函数

函数 返回值
system() 操作系统名称
node() 服务器的主机名,不完全合格的
release() 操作系统版本号
version() 更详细的系统版本
machine() 硬件类型标识符,例如 i386
processor() 处理器的真实标识符(在很多情况下与 machine() 的值相同)

platform_os_info.py

import platform

print('uname:', platform.uname())

print()
print('system   :', platform.system())
print('node     :', platform.node())
print('release  :', platform.release())
print('version  :', platform.version())
print('machine  :', platform.machine())
print('processor:', platform.processor())

OS X :

$ python3 platform_os_info.py

uname: uname_result(system='Darwin', node='hubert.local',
release='17.4.0', version='Darwin Kernel Version 17.4.0: Sun Dec
17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64',
machine='x86_64', processor='i386')

system   : Darwin
node     : hubert.local
release  : 17.4.0
version  : Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST
2017; root:xnu-4570.41.2~1/RELEASE_X86_64
machine  : x86_64
processor: i386

Linux :

$ python3 platform_os_info.py

uname: uname_result(system='Linux', node='apu',
release='3.13.0-55-generic', version='#94-Ubuntu SMP Thu Jun 18
00:27:10 UTC 2015', machine='x86_64', processor='x86_64')

system   : Linux
node     : apu
release  : 3.13.0-55-generic
version  : #94-Ubuntu SMP Thu Jun 18 00:27:10 UTC 2015
machine  : x86_64
processor: x86_64

Windows :

C:\>Desktop\platform_os_info.py

uname: uname_result(system='Windows', node='IE11WIN10',
release='10', version='10.0.10240', machine='AMD64',
processor='Intel64 Family 6 Model 70 Stepping 1, GenuineIntel')

system   : Windows
node     : IE11WIN10
release  : 10
version  : 10.0.10240
machine  : AMD64
processor: Intel64 Family 6 Model 70 Stepping 1, GenuineIntel

可执行架构

可以使用 architecture() 函数探测单个程序体系结构信息。 第一个参数是可执行程序的路径(默认为 sys.executable , Python 解释器)。 返回值是包含位体系结构和使用的链接格式的元组。

platform_architecture.py

import platform

print('interpreter:', platform.architecture())
print('/bin/ls    :', platform.architecture('/bin/ls'))

OS X :

$ python3 platform_architecture.py

interpreter: ('64bit', '')
/bin/ls    : ('64bit', '')

Linux :

$ python3 platform_architecture.py

interpreter: ('64bit', 'ELF')
/bin/ls    : ('64bit', 'ELF')

Windows :

C:\>Desktop\platform_architecture.py

interpreter: ('64bit', 'WindowsPE')
/bin/ls    : ('64bit', '')

另请参阅

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/pymotw/platform...

译文地址:https://learnku.com/docs/pymotw/platform...

上一篇 下一篇
贡献者:1
讨论数量: 0
发起讨论 只看当前版本


暂无话题~