-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[OS] title: Proposal: Enabling Android/Termux Support in psutil #2743
Description
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/filesystemsis often blocked.net_io_counters: Access to/proc/net/devis blocked on many modern Android versions.sensors_battery: Access to/sys/class/power_supplyis 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 crashing3. Empirical Test Results (Termux / API 30)
- Non-Rooted: Core APIs like
cpu_count,cpu_percent,virtual_memory, andboot_timework perfectly out of the box once the platform check is bypassed. - Rooted (
sudo): Running with elevated permissions resolves mostPermissionErrorissues.- Tests Passed: 129
- Tests Failed: 22 (mostly due to missing CLI tools like
whoorvmstatnot 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 tolen(freqs) <= cpu_count(). -
Battery Scaling: Some Android kernels report values in
/sys/class/power_supplythat can lead to$>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.