platform — Access to underlying platform’s identifying data

Source code:Lib/platform.py


Note

Specific platforms listed alphabetically, with Linux included in the Unixsection.

Cross platform

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

Queries the given executable (defaults to the Python interpreter binary) forvarious architecture information.

Returns a tuple(bits,linkage) which contain information about the bitarchitecture and the linkage format used for the executable. Both values arereturned 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 thesupported pointer size.

The function relies on the system’sfile command to do the actual work.This is available on most if not all Unix platforms and some non-Unix platformsand then only if the executable points to the Python interpreter. Reasonabledefaults are used when the above needs are not met.

Note

On macOS (and perhaps other platforms), executable files may beuniversal files containing multiple architectures.

To get at the “64-bitness” of the current interpreter, it is morereliable to query thesys.maxsize attribute:

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

Returns the machine type, e.g.'AMD64'. An empty string is returned if thevalue cannot be determined.

platform.node()

Returns the computer’s network name (may not be fully qualified!). An emptystring is returned if the value cannot be determined.

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

Returns a single string identifying the underlying platform with as much usefulinformation as possible.

The output is intended to behuman readable rather than machine parseable. Itmay look different on different platforms and this is intended.

Ifaliased is true, the function will use aliases for various platforms thatreport system names which differ from their common names, for example SunOS willbe reported as Solaris. Thesystem_alias() function is used to implementthis.

Settingterse to true causes the function to return only the absolute minimuminformation needed to identify the platform.

Changed in version 3.8:On macOS, the function now usesmac_ver(), if it returns anon-empty release string, to get the macOS version rather than the darwinversion.

platform.processor()

Returns the (real) processor name, e.g.'amdk6'.

An empty string is returned if the value cannot be determined. Note that manyplatforms do not provide this information or simply return the same value as formachine(). NetBSD does this.

platform.python_build()

Returns a tuple(buildno,builddate) stating the Python build number anddate as strings.

platform.python_compiler()

Returns a string identifying the compiler used for compiling Python.

platform.python_branch()

Returns a string identifying the Python implementation SCM branch.

platform.python_implementation()

Returns a string identifying the Python implementation. Possible return valuesare: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.

platform.python_revision()

Returns a string identifying the Python implementation SCM revision.

platform.python_version()

Returns the Python version as string'major.minor.patchlevel'.

Note that unlike the Pythonsys.version, the returned value will alwaysinclude the patchlevel (it defaults to 0).

platform.python_version_tuple()

Returns the Python version as tuple(major,minor,patchlevel) of strings.

Note that unlike the Pythonsys.version, the returned value will alwaysinclude the patchlevel (it defaults to'0').

platform.release()

Returns the system’s release, e.g.'2.2.0' or'NT'. An empty string isreturned 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.

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'), useos.uname().

platform.system_alias(system,release,version)

Returns(system,release,version) aliased to common marketing names usedfor some systems. It also does some reordering of the information in some caseswhere it would otherwise cause confusion.

platform.version()

Returns the system’s release version, e.g.'#3ondegas'. An empty string isreturned if the value cannot be determined.

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

platform.uname()

Fairly portable uname interface. Returns anamedtuple()containing six attributes:system,node,release,version,machine, andprocessor.

processor is resolved late, on demand.

Note: the first two attribute names differ from the names presented byos.uname(), where they are namedsysname andnodename.

Entries which cannot be determined are set to''.

Changed in version 3.3:Result changed from a tuple to anamedtuple().

Changed in version 3.9:processor is resolved late instead of immediately.

Java platform

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

Version interface for Jython.

Returns a tuple(release,vendor,vminfo,osinfo) withvminfo being atuple(vm_name,vm_release,vm_vendor) andosinfo being a tuple(os_name,os_version,os_arch). Values which cannot be determined are set tothe defaults given as parameters (which all default to'').

Deprecated since version 3.13, will be removed in version 3.15:It was largely untested, had a confusing API,and was only useful for Jython support.

Windows platform

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 whichcannot be determined are set to the defaults given as parameters (which alldefault to an empty string).

As a hint:ptype is'UniprocessorFree' on single processor NT machinesand'MultiprocessorFree' on multi processor machines. The'Free' refersto 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, orNone if thevalue cannot be determined. Possible values include but are not limited to'Enterprise','IoTUAP','ServerStandard', and'nanoserver'.

Added in version 3.8.

platform.win32_is_iot()

ReturnTrue if the Windows edition returned bywin32_edition()is recognized as an IoT edition.

Added in version 3.8.

macOS platform

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

Get macOS version information and return it as tuple(release,versioninfo,machine) withversioninfo being a tuple(version,dev_stage,non_release_version).

Entries which cannot be determined are set to''. All tuple entries arestrings.

iOS platform

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

Get iOS version information and return it as anamedtuple() 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 asimulator or a physical device.

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

Unix platforms

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

Tries to determine the libc version against which the file executable (defaultsto 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 versionsadd symbols to the executable is probably only usable for executables compiledusinggcc.

The file is read and scanned in chunks ofchunksize bytes.

Linux platforms

platform.freedesktop_os_release()

Get operating system identification fromos-release file and returnit as a dict. Theos-release file is afreedesktop.org standard andis available in most Linux distributions. A noticeable exception isAndroid and Android-based distributions.

RaisesOSError 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 arestrings. Values have their special characters like" and$unquoted. The fieldsNAME,ID, andPRETTY_NAME are alwaysdefined according to the standard. All other fields are optional. Vendorsmay include additional fields.

Note that fields likeNAME,VERSION, andVARIANT are stringssuitable for presentation to users. Programs should use fields likeID,ID_LIKE,VERSION_ID, orVARIANT_ID to identifyLinux distributions.

Example:

defget_like_distro():info=platform.freedesktop_os_release()ids=[info["ID"]]if"ID_LIKE"ininfo:# ids are space separated and ordered by precedenceids.extend(info["ID_LIKE"].split())returnids

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 anamedtuple()with the following attributes. Values which cannot be determined are set tothe 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.34for Android 14). To get the API level which Python was built against, seesys.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’sa physical device.

Google maintains alist of known model and device names.

Added in version 3.13.

Command-line usage

platform can also be invoked directly using the-mswitch of the interpreter:

python-mplatform[--terse][--nonaliased][{nonaliased,terse}...]

The following options are accepted:

--terse

Print terse information about the platform. This is equivalent tocallingplatform.platform() with theterse argument set toTrue.

--nonaliased

Print platform information without system/OS name aliasing. This isequivalent to callingplatform.platform() with thealiased argumentset toTrue.

You can also pass one or more positional arguments (terse,nonaliased)to explicitly control the output format. These behave similarly to theircorresponding options.