How to get UUID with Python? System Report will display the system's Hardware UUID in the format 01234567-89ab-cdef-0123-456789abcdef.

Using the shell we can retrieve the same UUID:


ioreg -rd1 -c IOPlatformExpertDevice | grep -E '(UUID)'


Is it possible to get this UUID in Python without resorting to executing a shell command?
Short answer: not really.

Long answer: sort of, but it's probably more trouble than it's worth, unless you really can't used the shell command for some reason. OS X doesn't really expose much system configuration to Python. Most scriptable interfaces to configure OS X are shell-based or AppleScript-based (and of course the various Objective-C APIs).

Getting the UUID is possible in AppleScript:

tell application "System Information"
    set profile to system profile
    quit
end tell

set delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "Hardware UUID: "
set uuid to paragraph 1 of text item 2 of profile
set AppleScript's text item delimiters to delim
uuid


But as you can see, it's a bit convoluted (and is much slower than the ioreg method, as it has to process the entire system profile).

There is a way to bridge AppleScript to Python, known as Appscript, but it's no longer developed, and not recommended for new projects. However, if you're really determined, you could convert the above AppleScript into Python using Appscript. But to be honest, it seems like a lot of trouble to get a worse result.