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 $>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.
After installing (sic, can be done) and testing
psutilon 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.platformreturns"android". The current logic strictly looks for"linux", which causessetup.pyand imports to fail immediately.Fix:
2. Graceful Degradation for Restricted Environments (
psutil/_pslinux.py)On Android, many
/procand/sysnodes are restricted to the shell user. Currently,psutilraisesPermissionError(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 PermissionErrorwraps: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:
3. Empirical Test Results (Termux / API 30)
cpu_count,cpu_percent,virtual_memory, andboot_timework perfectly out of the box once the platform check is bypassed.sudo): Running with elevated permissions resolves mostPermissionErrorissues.whoorvmstatnot being in the standard path/format).4. Special Considerations for ARM/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()./sys/class/power_supplythat can lead toSummary
By simply allowing the
"android"platform string and adding a few targetedPermissionErrorhandlers,psutilbecomes a viable tool for the millions of developers using Termux and Android-based edge devices.