Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen to use os.name, sys.platform, or platform.system?
    primarykey
    data
    text
    <p>As far as I know, Python has 3 ways of finding out what operating system is running on:</p> <ol> <li><code>os.name</code></li> <li><code>sys.platform</code></li> <li><code>platform.system()</code></li> </ol> <p>Knowing this information is often useful in conditional imports, or using functionality that differs between platforms (e.g. <code>time.clock()</code> on Windows v.s. <code>time.time()</code> on UNIX).</p> <p>My question is, why 3 different ways of doing this? When should one way be used and not another? Which way is the 'best' (most future-proof or least likely to accidentally exclude a particular system which your program can actually run on)?</p> <p>It seems like <code>sys.platform</code> is more specific than <code>os.name</code>, allowing you to distinguish <code>win32</code> from <code>cygwin</code> (as opposed to just <code>nt</code>), and <code>linux2</code> from <code>darwin</code> (as opposed to just <code>posix</code>). But if that's so, that what about the difference between <code>sys.platform</code> and <code>platform.system()</code>?</p> <p>For example, which is better, this:</p> <pre><code>import sys if sys.platform == 'linux2': # Do Linux-specific stuff </code></pre> <p>or this? :</p> <pre><code>import platform if platform.system() == 'Linux': # Do Linux-specific stuff </code></pre> <p>For now I'll be sticking to <code>sys.platform</code>, so this question isn't particularly urgent, but I would be very grateful for some clarification regarding this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload