[system-parse-lib]
A small Python library for reading system information on Linux.
system-parse-lib reads your OS name, kernel version, uptime, desktop environment, session type, CPU and GPU models and vendors, and basic user details, and hands them back as plain Python values. It's one module, systemparselib.py. It has no third-party dependencies and doesn't call any external commands: everything is read straight from /etc, /proc and /sys.
Requirements
- Linux
- Python 3.10 or newer
- Optional: a
pci.idsfile (from thehwdataorpciidspackage) soget_gpu()can show real model names
Installation
Install it from PyPI:
pip install system-parse-lib
Or use the source directly: clone the repository and copy systemparselib.py next to your script.
git clone https://github.com/black-mesa-studios/system-parse-lib.git
cp system-parse-lib/systemparselib.py ./
Quick start
import systemparselib
sysinfo = systemparselib.Main()
print(sysinfo.get_pretty_name()) # e.g. Arch Linux
print(sysinfo.get_de()) # e.g. KDE
print(sysinfo.get_session_type()) # e.g. wayland
print(sysinfo.get_kernel_version()) # e.g. 6.11.2-arch1-1
print(sysinfo.get_pretty_uptime()) # e.g. up 3 hours, 12 minutes
print(sysinfo.get_gpu()) # [GPU(vendor='AMD', model='...', driver='amdgpu')]
print(f"{sysinfo.get_username()}@{sysinfo.get_hostname()}")
Functions
Everything lives on the Main class. Creating an instance reads /etc/os-release once; each method then reads its own data when called.
| Method | Returns | Source |
|---|---|---|
get_distro_id() | str | None | ID in /etc/os-release |
get_pretty_name() | str | None | PRETTY_NAME in /etc/os-release |
get_username() | str | getpass.getuser() |
get_hostname() | str | socket.gethostname() |
get_de() | str | None | Environment variables |
get_session_type() | str | XDG_SESSION_TYPE |
get_kernel_version() | str | os.uname().release |
get_uptime_seconds() | int | None | /proc/uptime |
get_pretty_uptime() | str | None | /proc/uptime |
get_cpu() | str | None | /proc/cpuinfo |
get_gpu() | list[GPU] | /sys/bus/pci/devices |
Distro
get_distro_id()
Returns the distro's short ID with quotes removed, such as arch, ubuntu or fedora. Useful for picking a package manager.
get_pretty_name()
Returns the human-readable OS name, such as Arch Linux or Ubuntu 24.04 LTS.
Both return None if /etc/os-release doesn't exist.
User & host
get_username()
Returns the name of the current user.
get_hostname()
Returns the machine's hostname.
Desktop & session
get_de()
Returns the desktop environment from XDG_CURRENT_DESKTOP, falling back to DESKTOP_SESSION. Returns None when neither is set, for example over SSH or in a TTY.
get_session_type()
Returns the session type from XDG_SESSION_TYPE, such as wayland, x11 or tty. If the variable isn't set, it returns the string "Could not retrieve session type.".
System
get_kernel_version()
Returns the kernel release string, such as 6.11.2-arch1-1.
get_uptime_seconds()
Returns the time since boot in whole seconds, read from /proc/uptime. Returns None if it can't be read.
get_pretty_uptime()
Returns the uptime as readable text, such as up 3 hours, 12 minutes or up 2 days, 1 hour, 5 minutes. Returns None if the uptime can't be read.
Hardware
get_cpu()
Returns the CPU model name from /proc/cpuinfo, such as AMD Ryzen 5 5600X 6-Core Processor. Returns None if the file doesn't exist, and an empty string if it has no model name line (some ARM systems).
get_gpu()
Returns a list with one GPU object per display device found in /sys/bus/pci/devices. It returns an empty list if there are none.
| Field | Type | Description |
|---|---|---|
vendor | str | NVIDIA, AMD or Intel. Any other vendor is shown as its raw PCI vendor ID, like 1234. |
model | str | Model name from pci.ids. Falls back to Device <id> if no pci.ids file is found. |
driver | str | None | Kernel driver in use, such as amdgpu or nvidia, or None if none is bound. |
[GPU(vendor='NVIDIA', model='GeForce GTX 1650', driver='nvidia')]
For a plain dictionary, use dataclasses.asdict(gpu).