What platform is the current Python program running on? This should tell us what operating system and CPU architecture if possible.
With the platform module, we can call functions that give us details about the current system. We can even determine which version of Python we are running.
This program uses several functions on the platform module, and prints their output. It also tests the result of system()
in a match statement.
processor()
method, we get a string
that indicates the identification of the current CPU.machine()
method, we get the string
"AMD64" which indicates a 64-bit x86 processor.System()
returns a string
like "Windows", "Linux" or "Darwin" (which refers to the MacOS kernel).system()
method in a match statement. We select each case with case statements, and print a message.python_version
method.import platform # Part 1: print the processor name. print(platform.processor()) # Part 2: print CPU type. print(platform.machine()) # Part 3: print the system name. print(platform.system()) # Part 4: test the system. match platform.system(): case "Windows": print("Currently running Windows") case "Linux": print("On Linux") case "Darwin": print("On MacOS") case _: print("Other") # Part 5: print the python version. print(platform.python_version())Intel64 Family 6 Model 154 Stepping 3, GenuineIntel AMD64 Windows Currently running Windows 3.12.7
Sometimes it may be useful to fetch information about the current platform, such as for ensuring a program is running on a supported system. The platform module is ideal for these cases.