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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With