• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`!platform` ---  Access to underlying platform's identifying data
2======================================================================
3
4.. module:: platform
5   :synopsis: Retrieves as much platform identifying data as possible.
6
7.. moduleauthor:: Marc-André Lemburg <mal@egenix.com>
8.. sectionauthor:: Bjorn Pettersen <bpettersen@corp.fairisaac.com>
9
10**Source code:** :source:`Lib/platform.py`
11
12--------------
13
14.. note::
15
16   Specific platforms listed alphabetically, with Linux included in the Unix
17   section.
18
19
20Cross Platform
21--------------
22
23
24.. function:: architecture(executable=sys.executable, bits='', linkage='')
25
26   Queries the given executable (defaults to the Python interpreter binary) for
27   various architecture information.
28
29   Returns a tuple ``(bits, linkage)`` which contain information about the bit
30   architecture and the linkage format used for the executable. Both values are
31   returned as strings.
32
33   Values that cannot be determined are returned as given by the parameter presets.
34   If bits is given as ``''``, the ``sizeof(pointer)`` (or
35   ``sizeof(long)`` on Python version < 1.5.2) is used as indicator for the
36   supported pointer size.
37
38   The function relies on the system's :file:`file` command to do the actual work.
39   This is available on most if not all Unix  platforms and some non-Unix platforms
40   and then only if the executable points to the Python interpreter.  Reasonable
41   defaults are used when the above needs are not met.
42
43   .. note::
44
45      On macOS (and perhaps other platforms), executable files may be
46      universal files containing multiple architectures.
47
48      To get at the "64-bitness" of the current interpreter, it is more
49      reliable to query the :data:`sys.maxsize` attribute::
50
51         is_64bits = sys.maxsize > 2**32
52
53
54.. function:: machine()
55
56   Returns the machine type, e.g. ``'AMD64'``. An empty string is returned if the
57   value cannot be determined.
58
59
60.. function:: node()
61
62   Returns the computer's network name (may not be fully qualified!). An empty
63   string is returned if the value cannot be determined.
64
65
66.. function:: platform(aliased=False, terse=False)
67
68   Returns a single string identifying the underlying platform with as much useful
69   information as possible.
70
71   The output is intended to be *human readable* rather than machine parseable. It
72   may look different on different platforms and this is intended.
73
74   If *aliased* is true, the function will use aliases for various platforms that
75   report system names which differ from their common names, for example SunOS will
76   be reported as Solaris.  The :func:`system_alias` function is used to implement
77   this.
78
79   Setting *terse* to true causes the function to return only the absolute minimum
80   information needed to identify the platform.
81
82   .. versionchanged:: 3.8
83      On macOS, the function now uses :func:`mac_ver`, if it returns a
84      non-empty release string, to get the macOS version rather than the darwin
85      version.
86
87
88.. function:: processor()
89
90   Returns the (real) processor name, e.g. ``'amdk6'``.
91
92   An empty string is returned if the value cannot be determined. Note that many
93   platforms do not provide this information or simply return the same value as for
94   :func:`machine`.  NetBSD does this.
95
96
97.. function:: python_build()
98
99   Returns a tuple ``(buildno, builddate)`` stating the Python build number and
100   date as strings.
101
102
103.. function:: python_compiler()
104
105   Returns a string identifying the compiler used for compiling Python.
106
107
108.. function:: python_branch()
109
110   Returns a string identifying the Python implementation SCM branch.
111
112
113.. function:: python_implementation()
114
115   Returns a string identifying the Python implementation. Possible return values
116   are: 'CPython', 'IronPython', 'Jython', 'PyPy'.
117
118
119.. function:: python_revision()
120
121   Returns a string identifying the Python implementation SCM revision.
122
123
124.. function:: python_version()
125
126   Returns the Python version as string ``'major.minor.patchlevel'``.
127
128   Note that unlike the Python ``sys.version``, the returned value will always
129   include the patchlevel (it defaults to 0).
130
131
132.. function:: python_version_tuple()
133
134   Returns the Python version as tuple ``(major, minor, patchlevel)`` of strings.
135
136   Note that unlike the Python ``sys.version``, the returned value will always
137   include the patchlevel (it defaults to ``'0'``).
138
139
140.. function:: release()
141
142   Returns the system's release, e.g. ``'2.2.0'`` or ``'NT'``. An empty string is
143   returned if the value cannot be determined.
144
145
146.. function:: system()
147
148   Returns the system/OS name, such as ``'Linux'``, ``'Darwin'``, ``'Java'``,
149   ``'Windows'``. An empty string is returned if the value cannot be determined.
150
151   On iOS and Android, this returns the user-facing OS name (i.e, ``'iOS``,
152   ``'iPadOS'`` or ``'Android'``). To obtain the kernel name (``'Darwin'`` or
153   ``'Linux'``), use :func:`os.uname`.
154
155.. function:: system_alias(system, release, version)
156
157   Returns ``(system, release, version)`` aliased to common marketing names used
158   for some systems.  It also does some reordering of the information in some cases
159   where it would otherwise cause confusion.
160
161
162.. function:: version()
163
164   Returns the system's release version, e.g. ``'#3 on degas'``. An empty string is
165   returned if the value cannot be determined.
166
167   On iOS and Android, this is the user-facing OS version. To obtain the
168   Darwin or Linux kernel version, use :func:`os.uname`.
169
170.. function:: uname()
171
172   Fairly portable uname interface. Returns a :func:`~collections.namedtuple`
173   containing six attributes: :attr:`system`, :attr:`node`, :attr:`release`,
174   :attr:`version`, :attr:`machine`, and :attr:`processor`.
175
176   :attr:`processor` is resolved late, on demand.
177
178   Note: the first two attribute names differ from the names presented by
179   :func:`os.uname`, where they are named :attr:`sysname` and
180   :attr:`nodename`.
181
182   Entries which cannot be determined are set to ``''``.
183
184   .. versionchanged:: 3.3
185      Result changed from a tuple to a :func:`~collections.namedtuple`.
186
187   .. versionchanged:: 3.9
188      :attr:`processor` is resolved late instead of immediately.
189
190
191Java Platform
192-------------
193
194
195.. function:: java_ver(release='', vendor='', vminfo=('','',''), osinfo=('','',''))
196
197   Version interface for Jython.
198
199   Returns a tuple ``(release, vendor, vminfo, osinfo)`` with *vminfo* being a
200   tuple ``(vm_name, vm_release, vm_vendor)`` and *osinfo* being a tuple
201   ``(os_name, os_version, os_arch)``. Values which cannot be determined are set to
202   the defaults given as parameters (which all default to ``''``).
203
204   .. deprecated-removed:: 3.13 3.15
205      It was largely untested, had a confusing API,
206      and was only useful for Jython support.
207
208
209Windows Platform
210----------------
211
212
213.. function:: win32_ver(release='', version='', csd='', ptype='')
214
215   Get additional version information from the Windows Registry and return a tuple
216   ``(release, version, csd, ptype)`` referring to OS release, version number,
217   CSD level (service pack) and OS type (multi/single processor). Values which
218   cannot be determined are set to the defaults given as parameters (which all
219   default to an empty string).
220
221   As a hint: *ptype* is ``'Uniprocessor Free'`` on single processor NT machines
222   and ``'Multiprocessor Free'`` on multi processor machines. The ``'Free'`` refers
223   to the OS version being free of debugging code. It could also state ``'Checked'``
224   which means the OS version uses debugging code, i.e. code that checks arguments,
225   ranges, etc.
226
227.. function:: win32_edition()
228
229   Returns a string representing the current Windows edition, or ``None`` if the
230   value cannot be determined.  Possible values include but are not limited to
231   ``'Enterprise'``, ``'IoTUAP'``, ``'ServerStandard'``, and ``'nanoserver'``.
232
233   .. versionadded:: 3.8
234
235.. function:: win32_is_iot()
236
237   Return ``True`` if the Windows edition returned by :func:`win32_edition`
238   is recognized as an IoT edition.
239
240   .. versionadded:: 3.8
241
242
243macOS Platform
244--------------
245
246.. function:: mac_ver(release='', versioninfo=('','',''), machine='')
247
248   Get macOS version information and return it as tuple ``(release, versioninfo,
249   machine)`` with *versioninfo* being a tuple ``(version, dev_stage,
250   non_release_version)``.
251
252   Entries which cannot be determined are set to ``''``.  All tuple entries are
253   strings.
254
255iOS Platform
256------------
257
258.. function:: ios_ver(system='', release='', model='', is_simulator=False)
259
260   Get iOS version information and return it as a
261   :func:`~collections.namedtuple` with the following attributes:
262
263   * ``system`` is the OS name; either ``'iOS'`` or ``'iPadOS'``.
264   * ``release`` is the iOS version number as a string (e.g., ``'17.2'``).
265   * ``model`` is the device model identifier; this will be a string like
266     ``'iPhone13,2'`` for a physical device, or ``'iPhone'`` on a simulator.
267   * ``is_simulator`` is a boolean describing if the app is running on a
268     simulator or a physical device.
269
270   Entries which cannot be determined are set to the defaults given as
271   parameters.
272
273
274Unix Platforms
275--------------
276
277.. function:: libc_ver(executable=sys.executable, lib='', version='', chunksize=16384)
278
279   Tries to determine the libc version against which the file executable (defaults
280   to the Python interpreter) is linked.  Returns a tuple of strings ``(lib,
281   version)`` which default to the given parameters in case the lookup fails.
282
283   Note that this function has intimate knowledge of how different libc versions
284   add symbols to the executable is probably only usable for executables compiled
285   using :program:`gcc`.
286
287   The file is read and scanned in chunks of *chunksize* bytes.
288
289
290Linux Platforms
291---------------
292
293.. function:: freedesktop_os_release()
294
295   Get operating system identification from ``os-release`` file and return
296   it as a dict. The ``os-release`` file is a `freedesktop.org standard
297   <https://www.freedesktop.org/software/systemd/man/os-release.html>`_ and
298   is available in most Linux distributions. A noticeable exception is
299   Android and Android-based distributions.
300
301   Raises :exc:`OSError` or subclass when neither ``/etc/os-release`` nor
302   ``/usr/lib/os-release`` can be read.
303
304   On success, the function returns a dictionary where keys and values are
305   strings. Values have their special characters like ``"`` and ``$``
306   unquoted. The fields ``NAME``, ``ID``, and ``PRETTY_NAME`` are always
307   defined according to the standard. All other fields are optional. Vendors
308   may include additional fields.
309
310   Note that fields like ``NAME``, ``VERSION``, and ``VARIANT`` are strings
311   suitable for presentation to users. Programs should use fields like
312   ``ID``, ``ID_LIKE``, ``VERSION_ID``, or ``VARIANT_ID`` to identify
313   Linux distributions.
314
315   Example::
316
317      def get_like_distro():
318          info = platform.freedesktop_os_release()
319          ids = [info["ID"]]
320          if "ID_LIKE" in info:
321              # ids are space separated and ordered by precedence
322              ids.extend(info["ID_LIKE"].split())
323          return ids
324
325   .. versionadded:: 3.10
326
327
328Android Platform
329----------------
330
331.. function:: android_ver(release="", api_level=0, manufacturer="", \
332                          model="", device="", is_emulator=False)
333
334   Get Android device information. Returns a :func:`~collections.namedtuple`
335   with the following attributes. Values which cannot be determined are set to
336   the defaults given as parameters.
337
338   * ``release`` - Android version, as a string (e.g. ``"14"``).
339
340   * ``api_level`` - API level of the running device, as an integer (e.g. ``34``
341     for Android 14). To get the API level which Python was built against, see
342     :func:`sys.getandroidapilevel`.
343
344   * ``manufacturer`` - `Manufacturer name
345     <https://developer.android.com/reference/android/os/Build#MANUFACTURER>`__.
346
347   * ``model`` - `Model name
348     <https://developer.android.com/reference/android/os/Build#MODEL>`__ –
349     typically the marketing name or model number.
350
351   * ``device`` - `Device name
352     <https://developer.android.com/reference/android/os/Build#DEVICE>`__ –
353     typically the model number or a codename.
354
355   * ``is_emulator`` - ``True`` if the device is an emulator; ``False`` if it's
356     a physical device.
357
358   Google maintains a `list of known model and device names
359   <https://storage.googleapis.com/play_public/supported_devices.html>`__.
360
361   .. versionadded:: 3.13
362