5

There are at least three ways to detect OS/platform in Python.

What is the ideal application for each method? When should one method be used over another?


EDIT: My specific use case is install-time and and run-time checking of dependencies. I don't want to install certain libraries in setup.py if I'm on "Windows" because it will require Visual Studio. Then, at run-time I want to check if the dependency is available (which it might not be on "Windows").

EDIT 2: It would also be great to see a short example of when each level of OS detail is required.

Jace Browning
  • 11,699
  • 10
  • 66
  • 90
  • One important question would be, why do you need to know? Adding platform-specific behavior in Python is usually unnecessary. – Steve Howard Sep 26 '13 at 01:41
  • @SteveHoward It could be for finding third party libraries, or user directories. It is oddity, considering Python encourages having one way to do things. –  Sep 26 '13 at 01:42
  • @SteveHoward my specific use case is install-time and and run-time checking of dependencies. I don't want to install certain libraries in `setup.py` if "Windows" because it will require Visual Studio. Then I want to check at run-time if the dependency is available (which it might not be on "Windows"). – Jace Browning Sep 26 '13 at 01:43
  • @LegoStormtroopr I just found that, but there is no accepted answer on that question and the highest voted does not clarify the use of `platform.system()`. – Jace Browning Sep 26 '13 at 01:45
  • For me, `os.name => 'nt'`, `sys.platform => 'win32'`, and `platform.system() => 'Windows'`. – tckmn Sep 26 '13 at 01:45
  • @SteveHoward Another possible option which I realised I'll use is including Windows7 specific windowing features (Live the live preview stuff) in an application. –  Sep 26 '13 at 01:46
  • @Doorknob ...but what should my Python library do with that information? (That's my question.) – Jace Browning Sep 26 '13 at 01:48
  • I don't know, I really don't know much Python. Just pointing the difference between them out. – tckmn Sep 26 '13 at 01:50
  • That question gives you the differences between the different calls. As for which one you *should* use, thats up to you and your application based on your need. –  Sep 26 '13 at 01:52

1 Answers1

7

It depends on how much information you need.

os.name will give you only a high-level idea of the environment you're on (e.g. POSIX vs. Windows NT) - not even an OS name. The documentation says:

See also sys.platform has a finer granularity. os.uname() gives system-dependent version information. The platform module provides detailed checks for the system’s identity.

sys.platform gives you a little more information, and can actually tell you if you're running Linux vs. FreeBSD, for example.

The platform module will give you the most information, down to the version of the operating system you're running under, and processor information.

So you should specify what exactly you're trying to accomplish, and then it will probably become clear which method is most appropriate.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328