Reliably detect Windows in Python
>>> 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
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
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.
>>> import platform
>>> platform.system()
'Windows'