Skip to content

[OS] title: Proposal: Enabling Android/Termux Support in psutil #2743

@Manamama

Description

@Manamama

After installing (sic, can be done) and testing psutil on a modern Android 11 environment (Termux/aarch64), I've identified the minimal changes required to resolve the "platform not supported" errors and allow the library to function reliably (both with and without root).

1. Platform Recognition (psutil/_common.py)

In modern Termux environments, sys.platform returns "android". The current logic strictly looks for "linux", which causes setup.py and imports to fail immediately.
Fix:

# psutil/_common.py
LINUX = sys.platform.startswith(("linux", "android"))

2. Graceful Degradation for Restricted Environments (psutil/_pslinux.py)

On Android, many /proc and /sys nodes are restricted to the shell user. Currently, psutil raises PermissionError (crashing the call). To make the library usable on non-rooted devices, these should be caught to allow "partial" system info retrieval.

Key areas for try...except PermissionError wraps:

  • disk_partitions: Access to /proc/filesystems is often blocked.
  • net_io_counters: Access to /proc/net/dev is blocked on many modern Android versions.
  • sensors_battery: Access to /sys/class/power_supply is restricted.

Example Fix:

def net_io_counters():
    try:
        with open_text(f"{get_procfs_path()}/net/dev") as f:
            lines = f.readlines()
    except PermissionError:
        return {}  # Return empty dict instead of crashing

3. Empirical Test Results (Termux / API 30)

  • Non-Rooted: Core APIs like cpu_count, cpu_percent, virtual_memory, and boot_time work perfectly out of the box once the platform check is bypassed.
  • Rooted (sudo): Running with elevated permissions resolves most PermissionError issues.
    • Tests Passed: 129
    • Tests Failed: 22 (mostly due to missing CLI tools like who or vmstat not being in the standard path/format).

4. Special Considerations for ARM/Android

  • CPU Frequency: On Big.LITTLE architectures (common on Android), cpu_freq() may return entries per cluster rather than per logical core (e.g., 2 frequency entries for 8 cores). Test assertions should be relaxed to len(freqs) <= cpu_count().
  • Battery Scaling: Some Android kernels report values in /sys/class/power_supply that can lead to $&gt;100%$ charge reports if not normalized.

Summary

By simply allowing the "android" platform string and adding a few targeted PermissionError handlers, psutil becomes a viable tool for the millions of developers using Termux and Android-based edge devices.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions