Georg Brandl | 0eaab97 | 2009-06-08 08:00:22 +0000 | [diff] [blame] | 1 | :mod:`platform` --- Access to underlying platform's identifying data |
| 2 | ===================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: platform |
| 5 | :synopsis: Retrieves as much platform identifying data as possible. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Antoine Pitrou | fbd4f80 | 2012-08-11 16:51:50 +0200 | [diff] [blame] | 7 | .. moduleauthor:: Marc-André Lemburg <mal@egenix.com> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Bjorn Pettersen <bpettersen@corp.fairisaac.com> |
| 9 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/platform.py` |
| 11 | |
| 12 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | .. note:: |
| 15 | |
| 16 | Specific platforms listed alphabetically, with Linux included in the Unix |
| 17 | section. |
| 18 | |
| 19 | |
| 20 | Cross 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. |
Andrew Svetlov | a2fe334 | 2012-08-11 21:14:08 +0300 | [diff] [blame] | 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 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | f259076 | 2010-12-21 18:49:01 +0000 | [diff] [blame] | 43 | .. note:: |
| 44 | |
| 45 | On Mac OS X (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 :attr:`sys.maxsize` attribute:: |
| 50 | |
| 51 | is_64bits = sys.maxsize > 2**32 |
| 52 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
| 54 | .. function:: machine() |
| 55 | |
| 56 | Returns the machine type, e.g. ``'i386'``. 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=0, terse=0) |
| 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 | |
| 83 | .. function:: processor() |
| 84 | |
| 85 | Returns the (real) processor name, e.g. ``'amdk6'``. |
| 86 | |
| 87 | An empty string is returned if the value cannot be determined. Note that many |
| 88 | platforms do not provide this information or simply return the same value as for |
| 89 | :func:`machine`. NetBSD does this. |
| 90 | |
| 91 | |
| 92 | .. function:: python_build() |
| 93 | |
| 94 | Returns a tuple ``(buildno, builddate)`` stating the Python build number and |
| 95 | date as strings. |
| 96 | |
| 97 | |
| 98 | .. function:: python_compiler() |
| 99 | |
| 100 | Returns a string identifying the compiler used for compiling Python. |
| 101 | |
| 102 | |
| 103 | .. function:: python_branch() |
| 104 | |
| 105 | Returns a string identifying the Python implementation SCM branch. |
| 106 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
| 108 | .. function:: python_implementation() |
| 109 | |
| 110 | Returns a string identifying the Python implementation. Possible return values |
Ezio Melotti | b351bcc | 2011-05-03 20:41:48 +0300 | [diff] [blame] | 111 | are: 'CPython', 'IronPython', 'Jython', 'PyPy'. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
| 114 | .. function:: python_revision() |
| 115 | |
| 116 | Returns a string identifying the Python implementation SCM revision. |
| 117 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | |
| 119 | .. function:: python_version() |
| 120 | |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 121 | Returns the Python version as string ``'major.minor.patchlevel'``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
| 123 | Note that unlike the Python ``sys.version``, the returned value will always |
| 124 | include the patchlevel (it defaults to 0). |
| 125 | |
| 126 | |
| 127 | .. function:: python_version_tuple() |
| 128 | |
| 129 | Returns the Python version as tuple ``(major, minor, patchlevel)`` of strings. |
| 130 | |
| 131 | Note that unlike the Python ``sys.version``, the returned value will always |
| 132 | include the patchlevel (it defaults to ``'0'``). |
| 133 | |
| 134 | |
| 135 | .. function:: release() |
| 136 | |
| 137 | Returns the system's release, e.g. ``'2.2.0'`` or ``'NT'`` An empty string is |
| 138 | returned if the value cannot be determined. |
| 139 | |
| 140 | |
| 141 | .. function:: system() |
| 142 | |
| 143 | Returns the system/OS name, e.g. ``'Linux'``, ``'Windows'``, or ``'Java'``. An |
| 144 | empty string is returned if the value cannot be determined. |
| 145 | |
| 146 | |
| 147 | .. function:: system_alias(system, release, version) |
| 148 | |
| 149 | Returns ``(system, release, version)`` aliased to common marketing names used |
| 150 | for some systems. It also does some reordering of the information in some cases |
| 151 | where it would otherwise cause confusion. |
| 152 | |
| 153 | |
| 154 | .. function:: version() |
| 155 | |
| 156 | Returns the system's release version, e.g. ``'#3 on degas'``. An empty string is |
| 157 | returned if the value cannot be determined. |
| 158 | |
| 159 | |
| 160 | .. function:: uname() |
| 161 | |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 162 | Fairly portable uname interface. Returns a :func:`~collections.namedtuple` |
| 163 | containing six attributes: :attr:`system`, :attr:`node`, :attr:`release`, |
| 164 | :attr:`version`, :attr:`machine`, and :attr:`processor`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 166 | Note that this adds a sixth attribute (:attr:`processor`) not present |
| 167 | in the :func:`os.uname` result. Also, the attribute names are different |
| 168 | for the first two attributes; :func:`os.uname` names them |
| 169 | :attr:`sysname` and :attr:`nodename`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
| 171 | Entries which cannot be determined are set to ``''``. |
| 172 | |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 173 | .. versionchanged:: 3.3 |
| 174 | Result changed from a tuple to a namedtuple. |
| 175 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | |
| 177 | Java Platform |
| 178 | ------------- |
| 179 | |
| 180 | |
| 181 | .. function:: java_ver(release='', vendor='', vminfo=('','',''), osinfo=('','','')) |
| 182 | |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 183 | Version interface for Jython. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 184 | |
| 185 | Returns a tuple ``(release, vendor, vminfo, osinfo)`` with *vminfo* being a |
| 186 | tuple ``(vm_name, vm_release, vm_vendor)`` and *osinfo* being a tuple |
| 187 | ``(os_name, os_version, os_arch)``. Values which cannot be determined are set to |
| 188 | the defaults given as parameters (which all default to ``''``). |
| 189 | |
| 190 | |
| 191 | Windows Platform |
| 192 | ---------------- |
| 193 | |
| 194 | |
| 195 | .. function:: win32_ver(release='', version='', csd='', ptype='') |
| 196 | |
| 197 | Get additional version information from the Windows Registry and return a tuple |
Georg Brandl | 6647a71 | 2013-10-06 19:19:18 +0200 | [diff] [blame] | 198 | ``(release, version, csd, ptype)`` referring to OS release, version number, |
| 199 | CSD level (service pack) and OS type (multi/single processor). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | |
| 201 | As a hint: *ptype* is ``'Uniprocessor Free'`` on single processor NT machines |
| 202 | and ``'Multiprocessor Free'`` on multi processor machines. The *'Free'* refers |
| 203 | to the OS version being free of debugging code. It could also state *'Checked'* |
| 204 | which means the OS version uses debugging code, i.e. code that checks arguments, |
| 205 | ranges, etc. |
| 206 | |
| 207 | .. note:: |
| 208 | |
Antoine Pitrou | f259076 | 2010-12-21 18:49:01 +0000 | [diff] [blame] | 209 | This function works best with Mark Hammond's |
Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 210 | :mod:`win32all` package installed, but also on Python 2.3 and |
| 211 | later (support for this was added in Python 2.6). It obviously |
| 212 | only runs on Win32 compatible platforms. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 | |
| 214 | |
| 215 | Win95/98 specific |
| 216 | ^^^^^^^^^^^^^^^^^ |
| 217 | |
Antoine Pitrou | 877766d | 2011-03-19 17:00:37 +0100 | [diff] [blame] | 218 | .. function:: popen(cmd, mode='r', bufsize=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 | |
| 220 | Portable :func:`popen` interface. Find a working popen implementation |
| 221 | preferring :func:`win32pipe.popen`. On Windows NT, :func:`win32pipe.popen` |
| 222 | should work; on Windows 9x it hangs due to bugs in the MS C library. |
| 223 | |
Victor Stinner | 1dfd380 | 2011-03-03 12:54:07 +0000 | [diff] [blame] | 224 | .. deprecated:: 3.3 |
| 225 | This function is obsolete. Use the :mod:`subprocess` module. Check |
| 226 | especially the :ref:`subprocess-replacements` section. |
| 227 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 | |
| 229 | Mac OS Platform |
| 230 | --------------- |
| 231 | |
| 232 | |
| 233 | .. function:: mac_ver(release='', versioninfo=('','',''), machine='') |
| 234 | |
| 235 | Get Mac OS version information and return it as tuple ``(release, versioninfo, |
| 236 | machine)`` with *versioninfo* being a tuple ``(version, dev_stage, |
| 237 | non_release_version)``. |
| 238 | |
| 239 | Entries which cannot be determined are set to ``''``. All tuple entries are |
| 240 | strings. |
| 241 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | |
| 243 | Unix Platforms |
| 244 | -------------- |
| 245 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | .. function:: libc_ver(executable=sys.executable, lib='', version='', chunksize=2048) |
| 247 | |
| 248 | Tries to determine the libc version against which the file executable (defaults |
| 249 | to the Python interpreter) is linked. Returns a tuple of strings ``(lib, |
| 250 | version)`` which default to the given parameters in case the lookup fails. |
| 251 | |
| 252 | Note that this function has intimate knowledge of how different libc versions |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 253 | add symbols to the executable is probably only usable for executables compiled |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | using :program:`gcc`. |
| 255 | |
| 256 | The file is read and scanned in chunks of *chunksize* bytes. |
| 257 | |