brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -m platform tells you which OS and CPU a machine has

Python
~/tips/tip-python-m-platform-os-info

python3 -m platform

Print the operating system, kernel and architecture of a machine in one command.

When I’m on an unfamiliar box and want to know what it runs before installing anything, Python can tell me in one line, and it works the same on Linux, macOS and Windows.

python3 -m platform
# Linux-6.8.0-45-generic-x86_64-with-glibc2.39

python3 -c 'import platform; print(platform.freedesktop_os_release()["PRETTY_NAME"])'
# Ubuntu 24.04.4 LTS

The first line packs the system, kernel release, CPU architecture and C library version into one string. That last bit matters when you’re picking a prebuilt binary: glibc versus musl decides whether a download will run on Alpine at all.

The second one reads /etc/os-release and gives you the distribution name, which python3 -m platform doesn’t show. platform.machine() gives just the architecture, like x86_64 or aarch64, which I find handy in scripts that choose what to download.

The freedesktop_os_release function needs Python 3.10 or newer and only works on Linux systems that have an os-release file. On macOS it raises OSError. Inside a container, you’re looking at the container’s distribution but the host’s kernel, so a Debian image on an Ubuntu host shows Debian with the Ubuntu kernel version.

Further reading: platform module

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link