18.5. platform — 系统版本信息 | python 运行时服务 |《python 3 标准库实例教程》| python 技术论坛-江南app体育官方入口

未匹配的标注

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

虽然 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', '')

另请参阅

    本文章首发在 江南app体育官方入口 网站上。

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

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

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

    上一篇 下一篇
    贡献者:1
    讨论数量: 0



    暂无话题~
    网站地图