When to use os.name, sys.platform, or platform.system?

Dived a bit into the source code.

The output of sys.platform and os.name are determined at compile time. platform.system() determines the system type at run time.

  • sys.platform is specified as a compiler define during the build configuration.
  • os.name checks whether certain os specific modules are available (e.g. posix, nt, …)
  • platform.system() actually runs uname and potentially several other functions to determine the system type at run time.

My suggestion:

  • Use os.name to check whether it’s a posix-compliant system.
  • Use sys.platform to check whether it’s a linux, cygwin, darwin, atheos, etc.
  • Use platform.system() if you don’t believe the other sources.

Leave a Comment