platform
— Access to underlying platform’s identifying data¶
Source code: Lib/platform.py
Note
Specific platforms listed alphabetically, with Linux included in the Unix section.按字母顺序列出的特定平台,Unix部分包含Linux。
Cross Platform跨平台¶
-
platform.
architecture
(executable=sys.executable, bits='', linkage='')¶ Queries the given executable (defaults to the Python interpreter binary) for various architecture information.
Returns a tuple返回一个(bits, linkage)
which contain information about the bit architecture and the linkage format used for the executable.(bits, linkage)
,其中包含有关位体系结构和用于可执行文件的链接格式的信息。Both values are returned as strings.这两个值都作为字符串返回。Values that cannot be determined are returned as given by the parameter presets.无法确定的值将根据参数预设返回。If bits is given as如果位给定为''
, thesizeof(pointer)
(orsizeof(long)
on Python version < 1.5.2) is used as indicator for the supported pointer size.''
,则sizeof(pointer)
(或者在Python版本<1.5.2的情况下为sizeof(long)
)用作支持的指针大小的指示符。The function relies on the system’s该函数依赖于系统的file
command to do the actual work.file
命令来完成实际工作。This is available on most if not all Unix platforms and some non-Unix platforms and then only if the executable points to the Python interpreter.这在大多数(如果不是所有的话)Unix平台和一些非Unix平台上可用,并且只有当可执行文件指向Python解释器时才可用。Reasonable defaults are used when the above needs are not met.当不满足上述需求时,将使用合理的默认值。Note
On macOS (and perhaps other platforms), executable files may be universal files containing multiple architectures.在macOS(或许还有其他平台)上,可执行文件可能是包含多个体系结构的通用文件。To get at the “64-bitness” of the current interpreter, it is more reliable to query the要获得当前解释器的“64位”,查询sys.maxsize
attribute:sys.maxsize
属性更可靠:is_64bits = sys.maxsize > 2**32
-
platform.
machine
()¶ Returns the machine type, e.g.返回机器类型,例如'i386'
.'i386'
。An empty string is returned if the value cannot be determined.如果无法确定值,则返回空字符串。
-
platform.
node
()¶ Returns the computer’s network name (may not be fully qualified!).返回计算机的网络名称(可能不是完全限定的!)。An empty string is returned if the value cannot be determined.如果无法确定值,则返回空字符串。
-
platform.
platform
(aliased=0, terse=0)¶ Returns a single string identifying the underlying platform with as much useful information as possible.返回一个字符串,该字符串用尽可能多的有用信息标识基础平台。The output is intended to be human readable rather than machine parseable.输出的目的是让人可读,而不是机器可解析。It may look different on different platforms and this is intended.它在不同的平台上看起来可能不同,这是有意的。If aliased is true, the function will use aliases for various platforms that report system names which differ from their common names, for example SunOS will be reported as Solaris.如果aliased为true
,则函数将为报告不同于其通用名称的系统名称的各种平台使用别名,例如,SunOS将报告为Solaris。Thesystem_alias()
function is used to implement this.system_alias()
函数用于实现此功能。Setting terse to true causes the function to return only the absolute minimum information needed to identify the platform.将terse
设置为true
会导致函数仅返回标识平台所需的最小绝对信息。Changed in version 3.8:版本3.8中更改:On macOS, the function now uses在macOS上,如果函数返回非空的发布字符串,则该函数现在使用mac_ver()
, if it returns a non-empty release string, to get the macOS version rather than the darwin version.mac_ver()
来获取macOS版本,而不是darwin版本。
-
platform.
processor
()¶ Returns the (real) processor name, e.g.返回(真实)处理器名称,例如'amdk6'
.'amdk6'
。An empty string is returned if the value cannot be determined.如果无法确定值,则返回空字符串。Note that many platforms do not provide this information or simply return the same value as for请注意,许多平台不提供此信息,或者只返回与machine()
.machine()
相同的值。NetBSD does this.NetBSD可以做到这一点。
-
platform.
python_build
()¶ Returns a tuple返回一个元组(buildno, builddate)
stating the Python build number and date as strings.(buildno, builddate)
,将Python内部版本号和日期表示为字符串。
-
platform.
python_compiler
()¶ Returns a string identifying the compiler used for compiling Python.返回一个字符串,该字符串标识用于编译Python的编译器。
-
platform.
python_branch
()¶ Returns a string identifying the Python implementation SCM branch.返回标识Python实现SCM分支的字符串。
-
platform.
python_implementation
()¶ Returns a string identifying the Python implementation.返回标识Python实现的字符串。Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.可能的返回值为:“CPython”、“IronPython“、”Jython‘、”PyPy“。
-
platform.
python_revision
()¶ Returns a string identifying the Python implementation SCM revision.返回标识Python实现SCM版本的字符串。
-
platform.
python_version
()¶ Returns the Python version as string将Python版本返回为字符串'major.minor.patchlevel'
.'major.minor.patchlevel'
。Note that unlike the Python注意,与Pythonsys.version
, the returned value will always include the patchlevel (it defaults to 0).sys.version
不同,返回的值将始终包括补丁级别(默认为0)。
-
platform.
python_version_tuple
()¶ Returns the Python version as tuple将Python版本返回为字符串的元组(major, minor, patchlevel)
of strings.(major, minor, patchlevel)
。Note that unlike the Python
sys.version
, the returned value will always include the patchlevel (it defaults to'0'
).
-
platform.
release
()¶ Returns the system’s release, e.g.
'2.2.0'
or'NT'
. An empty string is returned if the value cannot be determined.
-
platform.
system
()¶ Returns the system/OS name, such as
'Linux'
,'Darwin'
,'Java'
,'Windows'
. An empty string is returned if the value cannot be determined.
-
platform.
system_alias
(system, release, version)¶ Returns
(system, release, version)
aliased to common marketing names used for some systems. It also does some reordering of the information in some cases where it would otherwise cause confusion.
-
platform.
version
()¶ Returns the system’s release version, e.g.
'#3 on degas'
. An empty string is returned if the value cannot be determined.
-
platform.
uname
()¶ Fairly portable uname interface. Returns a
namedtuple()
containing six attributes:system
,node
,release
,version
,machine
, andprocessor
.Note that this adds a sixth attribute (
processor
) not present in theos.uname()
result. Also, the attribute names are different for the first two attributes;os.uname()
names themsysname
andnodename
.Entries which cannot be determined are set to
''
.Changed in version 3.3:版本3.3中更改:Result changed from a tuple to a结果从元组更改为namedtuple()
.namedtuple()
。
Java Platform¶
-
platform.
java_ver
(release='', vendor='', vminfo='', '', '', osinfo='', '', '')¶ Version interface for Jython.
Returns a tuple
(release, vendor, vminfo, osinfo)
with vminfo being a tuple(vm_name, vm_release, vm_vendor)
and osinfo being a tuple(os_name, os_version, os_arch)
.Values which cannot be determined are set to the defaults given as parameters (which all default to无法确定的值被设置为作为参数给定的默认值(所有默认值都为''
).''
)。
Windows PlatformWindows平台¶
-
platform.
win32_ver
(release='', version='', csd='', ptype='')¶ Get additional version information from the Windows Registry and return a tuple
(release, version, csd, ptype)
referring to OS release, version number, CSD level (service pack) and OS type (multi/single processor). Values which cannot be determined are set to the defaults given as parameters (which all default to an empty string).As a hint: ptype is
'Uniprocessor Free'
on single processor NT machines and'Multiprocessor Free'
on multi processor machines. The ‘Free’ refers to the OS version being free of debugging code. It could also state ‘Checked’ which means the OS version uses debugging code, i.e. code that checks arguments, ranges, etc.
-
platform.
win32_edition
()¶ Returns a string representing the current Windows edition, or
None
if the value cannot be determined. Possible values include but are not limited to'Enterprise'
,'IoTUAP'
,'ServerStandard'
, and'nanoserver'
.New in version 3.8.版本3.8中新增。
-
platform.
win32_is_iot
()¶ Return
True
if the Windows edition returned bywin32_edition()
is recognized as an IoT edition.New in version 3.8.版本3.8中新增。
macOS Platform¶
-
platform.
mac_ver
(release='', versioninfo='', '', '', machine='')¶ Get macOS version information and return it as tuple
(release, versioninfo, machine)
with versioninfo being a tuple(version, dev_stage, non_release_version)
.Entries which cannot be determined are set to无法确定的条目设置为''
.''
。All tuple entries are strings.所有元组条目都是字符串。
Unix Platforms¶
-
platform.
libc_ver
(executable=sys.executable, lib='', version='', chunksize=16384)¶ Tries to determine the libc version against which the file executable (defaults to the Python interpreter) is linked. Returns a tuple of strings
(lib, version)
which default to the given parameters in case the lookup fails.Note that this function has intimate knowledge of how different libc versions add symbols to the executable is probably only usable for executables compiled using gcc.注意,这个函数非常了解不同的libc版本如何向可执行文件添加符号,这可能只适用于使用gcc编译的可执行文件。The file is read and scanned in chunks of chunksize bytes.该文件以chunksize字节块的形式读取和扫描。
Linux Platforms¶
-
platform.
freedesktop_os_release
()¶ Get operating system identification from
os-release
file and return it as a dict. Theos-release
file is a freedesktop.org standard and is available in most Linux distributions. A noticeable exception is Android and Android-based distributions.Raises
OSError
or subclass when neither/etc/os-release
nor/usr/lib/os-release
can be read.On success, the function returns a dictionary where keys and values are strings. Values have their special characters like
"
and$
unquoted. The fieldsNAME
,ID
, andPRETTY_NAME
are always defined according to the standard. All other fields are optional. Vendors may include additional fields.Note that fields like
NAME
,VERSION
, andVARIANT
are strings suitable for presentation to users. Programs should use fields likeID
,ID_LIKE
,VERSION_ID
, orVARIANT_ID
to identify Linux distributions.Example:
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 idsNew in version 3.10.版本3.10中新增。