89DEVs

Python: How to check Operating System?

how to check the operating system in Python?

To check the operating system in Python, use the platform, sys or os module of Python as shown below. The os module provides the most detailed information of any module. It includes the operating system name, the name of the machine on the network, the release date, the version and the hardware identifier. It is not possible to check the operating system or the os version using built-in function. It is necessary to import a module like platform or sys first.

check operating system using platform module

To check the operating system in Python, the platform module can be used as shown below. It provides different information about the underlying hardware of computer used to run Python. # check operating system using platform module import platform mySystem = platform.system() myPlatform = platform.platform() print(mySystem) print(myPlatform) In this example, we retrieve different information about the underlying computer System. The system name and the platform, which gives us information about the operating system, the version and the architecture. First, the platform module is imported. Then, the system() method is used to retrieve system information and the information is assigned to the variable mySystem. Then, the platform() method is used to retrieve information about the used platform and it is assigned to the variable myPlatform. Finally, both variables are printed as shown below. Darwin macOS-10.16-x86_64-i386-64bit

check operating system using sys module

To check the operating system of the underlying hardware in Python, use the sys module as shown below. # check operating system using sys module import sys myOS = sys.platform print(myOS) In this example, we check the operating system of the underlying hardware that runs Python. First the sys module is imported. Then, the attribute sys.platform is assigned to the variable myOS. Finally, the variable myOS is printed and the result is returned as shown below. darwin

check operating system using os module

To check the operating system of Python, use the os module as shown below. The advantage of the os module is that it shows the most detailed information of all modules. # check operating system using os module import os myOS = os.uname() print(myOS) In this example, the os module is used to retrieve detailed information about the underlying operating system. First, the os module is imported. Then, the uname() method of the os module is used to retrieve information identifying the operating system. Five attributes are returned:
  • sysname is the name of the operating system
  • nodename is the name of the machine on the network
  • release is the release version of the operating system
  • version is the kernel version and the release date
  • machine is the hardware identifier
Finally, the variable myOS is printed and the result is shown below. posix.uname_result(sysname='Darwin', nodename='mbp.lan', release='21.1.0', version='Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE_X86_64', machine='x86_64') To retrieve only a specific attribute use os module as shown below. # check operating system using os module import os myOS = os.uname() print(myOS.sysname) In this case, only the attribute sysname is printed. The result is shown below. Darwin

return values

The return values indicate the used operating systems in the following way:
return value meaning
win32 MS Windows (Win 32)
cygwin MS Windows (cygwin)
darwin Apple macOS
linux Linux Distributions like ubuntu, debian ...
aix AIX

                
        

Summary


Click to jump to section