Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use os.name vs. sys.platform vs. platform.system()? [duplicate]

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.

like image 264
Jace Browning Avatar asked Sep 26 '13 01:09

Jace Browning


1 Answers

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.

like image 181
Jonathon Reinhart Avatar answered Sep 30 '22 20:09

Jonathon Reinhart