Platform. 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.
Example. This program uses several functions on the platform module, and prints their output. It also tests the result of system() in a match statement.
Part 1 For the processor() method, we get a string that indicates the identification of the current CPU.
Part 2 For the machine() method, we get the string "AMD64" which indicates a 64-bit x86 processor.
Part 3 System() returns a string like "Windows", "Linux" or "Darwin" (which refers to the MacOS kernel).
Part 4 Here we use the system() method in a match statement. We select each case with case statements, and print a message.
Part 5 It is possible to extract the version of Python being run with the 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
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.