os.uname

The kernel view of the system, straight from uname(2): sysname ("Linux", "Darwin"), the host name, the kernel release and build string, and the hardware name. Unix only: os.uname does not exist on Windows (os.uname_result does). For portable code use platform.uname() or platform.system().

os functionPython 3.3+ (named fields)
Common call
os.uname().release
Returns
uname_result(sysname='Linux', nodename=..., release=..., version=..., machine=...)
Replaces
running the uname -a shell command
Watch out
Missing on Windows: use platform.uname() for portable code
os.uname()
→ os.uname_result

Common patterns

Portable system name
platform works on every OS and returns "Windows", "Linux" or "Darwin".
import platform
if platform.system() == 'Darwin':
    ...
Kernel version as a comparable tuple
The release string has a numeric prefix and a free-form suffix.
import os, re
major, minor = map(int, re.match(r'(\d+)\.(\d+)', os.uname().release).groups())
has_feature = (major, minor) >= (5, 8)
Architecture check
machine is the hardware name, e.g. x86_64 or arm64 / aarch64.
import os
is_arm = os.uname().machine in ('arm64', 'aarch64')

Examples

1. Five named fields
import os u = os.uname_result(('Linux', 'web1', '6.8.0-45-generic', '#45-Ubuntu SMP', 'x86_64')) (u.sysname, u.release, u.machine)
Returns
('Linux', '6.8.0-45-generic', 'x86_64')
2. Also a tuple
import os u = os.uname_result(('Linux', 'web1', '6.8.0', '#1', 'x86_64')) (u[0], len(u))
Returns
('Linux', 5)
3. Field count
import os os.uname_result.n_fields
Returns
5
4. os.uname exists only on POSIX
import os hasattr(os, 'uname') == (os.name == 'posix')
Returns
True
5. platform.uname has a sixth field
import platform platform.uname()._fields
Returns
('system', 'node', 'release', 'version', 'machine', 'processor')
6. platform.system agrees with platform.uname
import platform platform.uname().system == platform.system()
Returns
True

Pitfalls

1. Comparing kernel releases as strings
release is text, and text compares character by character: "6.10" sorts before "6.9". Parse the numbers first.
string compare
release = '6.10.0-18-amd64'
release >= '6.9'
False
tuple of ints
import re
release = '6.10.0-18-amd64'
tuple(map(int, re.match(r'(\d+)\.(\d+)', release).groups())) >= (6, 9)
True
2. Feature-testing the result type instead of the function
os.uname_result is defined on Windows too, but os.uname is not. Test for the function (or check os.name) before calling it.
hasattr(os, 'uname_result')
import os
hasattr(os, 'uname_result')  # True even on Windows
True
hasattr(os, 'uname')
import os, platform
info = os.uname() if hasattr(os, 'uname') else platform.uname()
len(info) >= 5
True

When to use

Use it
  • Logging the kernel release and machine of a Unix server
  • Kernel-version checks before using a Linux-specific feature
Reach for something else
  • Code that must run on Windows → platform.uname() / platform.system()
  • Choosing code paths by OS → sys.platform (finer granularity, no call needed)
  • The host name only → socket.gethostname()
  • Distribution name and version (Ubuntu 24.04 ...) → platform.freedesktop_os_release()

Notes

CPython impl
Wraps the uname(2) system call; the fields are the members of struct utsname
Availability
os.uname: Unix only. os.uname_result exists on every platform
macOS, iOS, Android
os.uname reports the kernel: "Darwin" on macOS and iOS, "Linux" on Android. platform.uname() gives the user-facing OS name and release on iOS and Android
Return type
A plain tuple before 3.3; a named struct sequence since

FAQ

os.uname is Unix only (docs: Availability: Unix), so on Windows it raises AttributeError, and on Unix its values describe the machine running it. The examples build uname_result values by hand and use platform for the portable parts.