| 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. | 
 | 6 | .. moduleauthor:: Marc-Andre Lemburg <mal@egenix.com> | 
 | 7 | .. sectionauthor:: Bjorn Pettersen <bpettersen@corp.fairisaac.com> | 
 | 8 |  | 
| Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/platform.py` | 
 | 10 |  | 
 | 11 | -------------- | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | .. note:: | 
 | 14 |  | 
 | 15 |    Specific platforms listed alphabetically, with Linux included in the Unix | 
 | 16 |    section. | 
 | 17 |  | 
 | 18 |  | 
 | 19 | Cross Platform | 
 | 20 | -------------- | 
 | 21 |  | 
 | 22 |  | 
 | 23 | .. function:: architecture(executable=sys.executable, bits='', linkage='') | 
 | 24 |  | 
 | 25 |    Queries the given executable (defaults to the Python interpreter binary) for | 
 | 26 |    various architecture information. | 
 | 27 |  | 
 | 28 |    Returns a tuple ``(bits, linkage)`` which contain information about the bit | 
 | 29 |    architecture and the linkage format used for the executable. Both values are | 
 | 30 |    returned as strings. | 
 | 31 |  | 
 | 32 |    Values that cannot be determined are returned as given by the parameter presets. | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 33 |    If bits is given as ``''``, the :c:func:`sizeof(pointer)` (or | 
 | 34 |    :c:func:`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] | 35 |    supported pointer size. | 
 | 36 |  | 
 | 37 |    The function relies on the system's :file:`file` command to do the actual work. | 
 | 38 |    This is available on most if not all Unix  platforms and some non-Unix platforms | 
 | 39 |    and then only if the executable points to the Python interpreter.  Reasonable | 
 | 40 |    defaults are used when the above needs are not met. | 
 | 41 |  | 
| Antoine Pitrou | f259076 | 2010-12-21 18:49:01 +0000 | [diff] [blame] | 42 |    .. note:: | 
 | 43 |  | 
 | 44 |       On Mac OS X (and perhaps other platforms), executable files may be | 
 | 45 |       universal files containing multiple architectures. | 
 | 46 |  | 
 | 47 |       To get at the "64-bitness" of the current interpreter, it is more | 
 | 48 |       reliable to query the :attr:`sys.maxsize` attribute:: | 
 | 49 |  | 
 | 50 |          is_64bits = sys.maxsize > 2**32 | 
 | 51 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 |  | 
 | 53 | .. function:: machine() | 
 | 54 |  | 
 | 55 |    Returns the machine type, e.g. ``'i386'``. An empty string is returned if the | 
 | 56 |    value cannot be determined. | 
 | 57 |  | 
 | 58 |  | 
 | 59 | .. function:: node() | 
 | 60 |  | 
 | 61 |    Returns the computer's network name (may not be fully qualified!). An empty | 
 | 62 |    string is returned if the value cannot be determined. | 
 | 63 |  | 
 | 64 |  | 
 | 65 | .. function:: platform(aliased=0, terse=0) | 
 | 66 |  | 
 | 67 |    Returns a single string identifying the underlying platform with as much useful | 
 | 68 |    information as possible. | 
 | 69 |  | 
 | 70 |    The output is intended to be *human readable* rather than machine parseable. It | 
 | 71 |    may look different on different platforms and this is intended. | 
 | 72 |  | 
 | 73 |    If *aliased* is true, the function will use aliases for various platforms that | 
 | 74 |    report system names which differ from their common names, for example SunOS will | 
 | 75 |    be reported as Solaris.  The :func:`system_alias` function is used to implement | 
 | 76 |    this. | 
 | 77 |  | 
 | 78 |    Setting *terse* to true causes the function to return only the absolute minimum | 
 | 79 |    information needed to identify the platform. | 
 | 80 |  | 
 | 81 |  | 
 | 82 | .. function:: processor() | 
 | 83 |  | 
 | 84 |    Returns the (real) processor name, e.g. ``'amdk6'``. | 
 | 85 |  | 
 | 86 |    An empty string is returned if the value cannot be determined. Note that many | 
 | 87 |    platforms do not provide this information or simply return the same value as for | 
 | 88 |    :func:`machine`.  NetBSD does this. | 
 | 89 |  | 
 | 90 |  | 
 | 91 | .. function:: python_build() | 
 | 92 |  | 
 | 93 |    Returns a tuple ``(buildno, builddate)`` stating the Python build number and | 
 | 94 |    date as strings. | 
 | 95 |  | 
 | 96 |  | 
 | 97 | .. function:: python_compiler() | 
 | 98 |  | 
 | 99 |    Returns a string identifying the compiler used for compiling Python. | 
 | 100 |  | 
 | 101 |  | 
 | 102 | .. function:: python_branch() | 
 | 103 |  | 
 | 104 |    Returns a string identifying the Python implementation SCM branch. | 
 | 105 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 |  | 
 | 107 | .. function:: python_implementation() | 
 | 108 |  | 
 | 109 |    Returns a string identifying the Python implementation. Possible return values | 
| Ezio Melotti | b351bcc | 2011-05-03 20:41:48 +0300 | [diff] [blame] | 110 |    are: 'CPython', 'IronPython', 'Jython', 'PyPy'. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 |  | 
 | 113 | .. function:: python_revision() | 
 | 114 |  | 
 | 115 |    Returns a string identifying the Python implementation SCM revision. | 
 | 116 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 |  | 
 | 118 | .. function:: python_version() | 
 | 119 |  | 
 | 120 |    Returns the Python version as string ``'major.minor.patchlevel'`` | 
 | 121 |  | 
 | 122 |    Note that unlike the Python ``sys.version``, the returned value will always | 
 | 123 |    include the patchlevel (it defaults to 0). | 
 | 124 |  | 
 | 125 |  | 
 | 126 | .. function:: python_version_tuple() | 
 | 127 |  | 
 | 128 |    Returns the Python version as tuple ``(major, minor, patchlevel)`` of strings. | 
 | 129 |  | 
 | 130 |    Note that unlike the Python ``sys.version``, the returned value will always | 
 | 131 |    include the patchlevel (it defaults to ``'0'``). | 
 | 132 |  | 
 | 133 |  | 
 | 134 | .. function:: release() | 
 | 135 |  | 
 | 136 |    Returns the system's release, e.g. ``'2.2.0'`` or ``'NT'`` An empty string is | 
 | 137 |    returned if the value cannot be determined. | 
 | 138 |  | 
 | 139 |  | 
 | 140 | .. function:: system() | 
 | 141 |  | 
 | 142 |    Returns the system/OS name, e.g. ``'Linux'``, ``'Windows'``, or ``'Java'``. An | 
 | 143 |    empty string is returned if the value cannot be determined. | 
 | 144 |  | 
 | 145 |  | 
 | 146 | .. function:: system_alias(system, release, version) | 
 | 147 |  | 
 | 148 |    Returns ``(system, release, version)`` aliased to common marketing names used | 
 | 149 |    for some systems.  It also does some reordering of the information in some cases | 
 | 150 |    where it would otherwise cause confusion. | 
 | 151 |  | 
 | 152 |  | 
 | 153 | .. function:: version() | 
 | 154 |  | 
 | 155 |    Returns the system's release version, e.g. ``'#3 on degas'``. An empty string is | 
 | 156 |    returned if the value cannot be determined. | 
 | 157 |  | 
 | 158 |  | 
 | 159 | .. function:: uname() | 
 | 160 |  | 
 | 161 |    Fairly portable uname interface. Returns a tuple of strings ``(system, node, | 
 | 162 |    release, version, machine, processor)`` identifying the underlying platform. | 
 | 163 |  | 
 | 164 |    Note that unlike the :func:`os.uname` function this also returns possible | 
 | 165 |    processor information as additional tuple entry. | 
 | 166 |  | 
 | 167 |    Entries which cannot be determined are set to ``''``. | 
 | 168 |  | 
 | 169 |  | 
 | 170 | Java Platform | 
 | 171 | ------------- | 
 | 172 |  | 
 | 173 |  | 
 | 174 | .. function:: java_ver(release='', vendor='', vminfo=('','',''), osinfo=('','','')) | 
 | 175 |  | 
| Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 176 |    Version interface for Jython. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 |  | 
 | 178 |    Returns a tuple ``(release, vendor, vminfo, osinfo)`` with *vminfo* being a | 
 | 179 |    tuple ``(vm_name, vm_release, vm_vendor)`` and *osinfo* being a tuple | 
 | 180 |    ``(os_name, os_version, os_arch)``. Values which cannot be determined are set to | 
 | 181 |    the defaults given as parameters (which all default to ``''``). | 
 | 182 |  | 
 | 183 |  | 
 | 184 | Windows Platform | 
 | 185 | ---------------- | 
 | 186 |  | 
 | 187 |  | 
 | 188 | .. function:: win32_ver(release='', version='', csd='', ptype='') | 
 | 189 |  | 
 | 190 |    Get additional version information from the Windows Registry and return a tuple | 
 | 191 |    ``(version, csd, ptype)`` referring to version number, CSD level and OS type | 
 | 192 |    (multi/single processor). | 
 | 193 |  | 
 | 194 |    As a hint: *ptype* is ``'Uniprocessor Free'`` on single processor NT machines | 
 | 195 |    and ``'Multiprocessor Free'`` on multi processor machines. The *'Free'* refers | 
 | 196 |    to the OS version being free of debugging code. It could also state *'Checked'* | 
 | 197 |    which means the OS version uses debugging code, i.e. code that checks arguments, | 
 | 198 |    ranges, etc. | 
 | 199 |  | 
 | 200 |    .. note:: | 
 | 201 |  | 
| Antoine Pitrou | f259076 | 2010-12-21 18:49:01 +0000 | [diff] [blame] | 202 |       This function works best with Mark Hammond's | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 203 |       :mod:`win32all` package installed, but also on Python 2.3 and | 
 | 204 |       later (support for this was added in Python 2.6). It obviously | 
 | 205 |       only runs on Win32 compatible platforms. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 |  | 
 | 207 |  | 
 | 208 | Win95/98 specific | 
 | 209 | ^^^^^^^^^^^^^^^^^ | 
 | 210 |  | 
| Antoine Pitrou | 877766d | 2011-03-19 17:00:37 +0100 | [diff] [blame] | 211 | .. function:: popen(cmd, mode='r', bufsize=-1) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 |  | 
 | 213 |    Portable :func:`popen` interface.  Find a working popen implementation | 
 | 214 |    preferring :func:`win32pipe.popen`.  On Windows NT, :func:`win32pipe.popen` | 
 | 215 |    should work; on Windows 9x it hangs due to bugs in the MS C library. | 
 | 216 |  | 
| Victor Stinner | 1dfd380 | 2011-03-03 12:54:07 +0000 | [diff] [blame] | 217 |    .. deprecated:: 3.3 | 
 | 218 |       This function is obsolete.  Use the :mod:`subprocess` module.  Check | 
 | 219 |       especially the :ref:`subprocess-replacements` section. | 
 | 220 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 221 |  | 
 | 222 | Mac OS Platform | 
 | 223 | --------------- | 
 | 224 |  | 
 | 225 |  | 
 | 226 | .. function:: mac_ver(release='', versioninfo=('','',''), machine='') | 
 | 227 |  | 
 | 228 |    Get Mac OS version information and return it as tuple ``(release, versioninfo, | 
 | 229 |    machine)`` with *versioninfo* being a tuple ``(version, dev_stage, | 
 | 230 |    non_release_version)``. | 
 | 231 |  | 
 | 232 |    Entries which cannot be determined are set to ``''``.  All tuple entries are | 
 | 233 |    strings. | 
 | 234 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 235 |  | 
 | 236 | Unix Platforms | 
 | 237 | -------------- | 
 | 238 |  | 
 | 239 |  | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 240 | .. function:: dist(distname='', version='', id='', supported_dists=('SuSE','debian','redhat','mandrake',...)) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 242 |    This is another name for :func:`linux_distribution`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 243 |  | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 244 | .. function:: linux_distribution(distname='', version='', id='', supported_dists=('SuSE','debian','redhat','mandrake',...), full_distribution_name=1) | 
 | 245 |  | 
 | 246 |    Tries to determine the name of the Linux OS distribution name. | 
 | 247 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 248 |    ``supported_dists`` may be given to define the set of Linux distributions to | 
 | 249 |    look for. It defaults to a list of currently supported Linux distributions | 
 | 250 |    identified by their release file name. | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 251 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 252 |    If ``full_distribution_name`` is true (default), the full distribution read | 
 | 253 |    from the OS is returned. Otherwise the short name taken from | 
 | 254 |    ``supported_dists`` is used. | 
| Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 255 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 256 |    Returns a tuple ``(distname,version,id)`` which defaults to the args given as | 
 | 257 |    parameters.  ``id`` is the item in parentheses after the version number.  It | 
 | 258 |    is usually the version codename. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 |  | 
 | 260 | .. function:: libc_ver(executable=sys.executable, lib='', version='', chunksize=2048) | 
 | 261 |  | 
 | 262 |    Tries to determine the libc version against which the file executable (defaults | 
 | 263 |    to the Python interpreter) is linked.  Returns a tuple of strings ``(lib, | 
 | 264 |    version)`` which default to the given parameters in case the lookup fails. | 
 | 265 |  | 
 | 266 |    Note that this function has intimate knowledge of how different libc versions | 
| Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 267 |    add symbols to the executable is probably only usable for executables compiled | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 |    using :program:`gcc`. | 
 | 269 |  | 
 | 270 |    The file is read and scanned in chunks of *chunksize* bytes. | 
 | 271 |  |