When to use os.name vs. sys.platform vs. platform.system ()?

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

What is the ideal application for each method? When should you use one method over another?


EDIT: My specific use case is installation time and dependency runtime. I do not want to install certain libraries in setup.py if I am in "Windows" because this will require Visual Studio. Then, at runtime, I want to check if the dependency is available (which may not be on "Windows").

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

+4
source share
1 answer

It depends on how much information you need.

os.name will give you only a high-level view of the environment you are in (for example, POSIX vs. Windows NT) - even the name of the operating system. The documentation says:

See also sys.platform has finer granularity. os.uname() provides system version information. The platform module provides detailed system identity checks.

sys.platform gives you a little more information and can actually tell you if you are using Linux or FreeBSD, for example.

The platform module will provide you with the most complete information, up to the version of the operating system in which you work, and information about the processor.

So, you must indicate what exactly you are trying to execute, and then it will probably become clear which method is most suitable.

+7
source

All Articles