68

I'm working on a couple of Linux tools and need to prevent installation on Windows, since it depends on FHS and is thus rendered useless on that platform. The platform.platform function comes close but only returns a string.

Unfortunately I don't know what to search for in that string for it to yield a reliable result. Does anyone know what to search for or does anyone know of another function that I'm missing here?

blokkie
  • 5,375
  • 7
  • 24
  • 18

6 Answers6

91
>>> import platform
>>> platform.system()
'Windows'
Matthew Iselin
  • 10,400
  • 4
  • 51
  • 62
71

For those that came here looking for a way to detect Cygwin from Python (as opposed to just detecting Windows), here are some example return values from os.name and platform.system on different platforms

OS/build     | os.name | platform.system() 
-------------+---------+-----------------------
Win32 native | nt      | Windows
Win32 cygwin | posix   | CYGWIN_NT-5.1*
Win64 native | nt      | Windows
Win64 cygwin | posix   | CYGWIN_NT-6.1-WOW64*
Linux        | posix   | Linux

From this point, how to distinguish between Windows native and Cygwin should be obvious although I'm not convinced this is future proof.

* version numbers are for XP and Win7 respectively, do not rely on them

sbk
  • 9,212
  • 4
  • 32
  • 40
  • @JaceBrowning: fair enough, we are probably using different version of Cygwin or Python... This is where the "not-futureproof" note comes in :) – sbk Jun 04 '12 at 13:28
  • the difference is probably that my Python installation is part of Windows, not through Cygwin? – Jace Browning Jun 04 '12 at 17:49
  • 1
    @JaceBrowning: which I call "native" in my post. What exactly is your exact output from `os.name` and `platform.system()`? – sbk Jun 06 '12 at 12:41
  • os.name=='nt' platform.system()=='Windows' – Jace Browning Jun 06 '12 at 16:58
  • 2
    So that's exactly what I listed in the table above for the "native" Python builds that go on Windows. No contradiction. – sbk Jun 07 '12 at 10:49
  • 1
    This is exactly what i was looking for. Thanks for posting this. – Cameron Goodale Mar 12 '13 at 15:49
  • 2
    Just worth nothing that the above only works when the python binary used is the cygwin binary and not a native windows one. – Richard Corden May 11 '15 at 08:25
  • `platform.system` can also return `'Java'` and `os.name` can also return `'posix'` `'os2'` `'ce'` `'java'` and `'riscos'` – endolith Jan 22 '17 at 20:58
15

On my Windows box, platform.system() returns 'Windows'.

However, I'm not sure why you'd bother. If you want to limit the platform it runs on technologically, I'd use a white-list rather than a black-list.

In fact, I wouldn't do it technologically at all since perhaps the next release of Python may have Win32/Win64 instead of Windows (for black-listing) and *nix instead of Linux (for white-listing).

My advice is to simply state what the requirements are and, if the user chooses to ignore that, that's their problem. If they ring up saying they got an error message stating "Cannot find FHS" and they admit they're running on Windows, gently point out to them that it's not a supported configuration.

Maybe your customers are smart enough to get FHS running under Windows so that your code will work. They're unlikely to appreciate what they would then consider an arbitrary limitation of your software.

This is a problem faced by software developers every day. Even huge organizations can't support every single platform and configuration out there.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    +1, good answer and I agree. Detecting that FHS isn't available is a far better method than detecting the OS and making an assumption, and it's safer (after all, having someone hack around your platform limit is going to fail *anyway*!). – Matthew Iselin Sep 07 '09 at 01:56
  • +1, yes a whitelist is preferable as the corner cases are nasty. Running under Cygwin on a Windows box I get `platform.system()` returning `CYGWIN_NT-5.2-WOW64`. That wouldn't have been my first guess! – Scott Griffiths Sep 07 '09 at 10:07
  • `ask for forgiveness not permission`. very good recommendation. Example: preemptive refusal of installer programs are the worst thing programmer can impose on us. E.g.: trying to be all clever and checking disk space is a big no no. I could be installing on a symlink. – v.oddou Oct 19 '18 at 07:45
13
>>> import os
>>> os.name
'nt'

"The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos'." (c) http://docs.python.org/library/os.html#os.name

import os
if os.name == 'nt':
    #yourcodehere
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
11

Try this:

import platform

if platform.system() == "Darwin":
    # Don't have Windows handy, but I'd expect "Win32" or "Windows" for it

Edit: Just saw that you tried platform.platform()...platform.system() will work better for this case. Trust me, use it. Dark corners lie in platform detection.

distutils will do this too, if you ask it nicely.

You could always do something bad like os.path.exists() on a Windows file...but platform is as reliable as it gets in the Python standard library.

Edit 2: Another helpful answerer pointed out platform.system() is exactly equal to "Windows" on his Windows machine.

Jed Smith
  • 15,584
  • 8
  • 52
  • 59
  • 1
    I also don't have Windows handy, but this function returns 'Linux' on my pc. The docs seem to mention 'Windows', so I guess I will just go with that and hope that it covers all cases. – blokkie Sep 07 '09 at 01:34
  • It should. See Pax's answer. – Jed Smith Sep 07 '09 at 01:35
1

From help(platform)

system()
    Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.

    An empty string is returned if the value cannot be determined.
boatcoder
  • 17,525
  • 18
  • 114
  • 178