8

I am running a python script in a termux environment on an Android device and I would like to be able to detect that the OS is Android.

The traditional approaches don't work:

>>> import platform
>>> import sys
>>> print(platform.system())
'Linux'
>>> print(sys.platform)
'linux'
>>> print(platform.release())
'4.14.117-perf+'
>>> print(platform.platform())
'Linux-4.14.117-perf+-aarch64-with-libc'

What other ootb options are available?

An apparently useful option is platform.machine() which returns armv8 — this is more than just 'Linux' yet it's just the architecture, and not the OS, and it might return a false positive for example on a raspberry pi or other arm-based systems.

2
  • Perhaps os.uname()? Commented Jun 1, 2020 at 14:40
  • 1
    @JohnGordon os.uname() returns linux. Commented Jun 1, 2020 at 14:46

2 Answers 2

3

I tried os.uname() without success. So I may suggest using subprocess since uname -o returns b'Android\n'.

Here is a simple check for Android:

import subprocess
subprocess.check_output(['uname', '-o']).strip() == b'Android'
Sign up to request clarification or add additional context in comments.

3 Comments

this could do, but this is not really a python-based solution, since it relies on an external utility and moreover spawns a new process
@ccpizza i searched web and tried different things, could not come-up with another solution. sorry for that.
thank you for taking the time to look into it! much appreciated!
2

There is more simple way that doesn't depend using external utilities and just uses sys module. Here is code:

import sys
is_android: bool = hasattr(sys, 'getandroidapilevel')

Here are it's pros and cons:

@@Pros@@
 + Does not depend on environment values
 + Does not depend on third-party modules
 + Simple one-liner (2 technically)

@@Cons@@
 - Version restriction (Supports CPython 3.7+ or equivalent)
 - Implementation-dependent (while CPython implements this I don't know about others)

9 Comments

I've seen ANDROID_DATA used for this too. It would be good to make a list of the default environment contents of different terminal emulators and sdks like kivy or beeware.
@fuzzyTew It's not always a good idea to use Environmental Variables for detection since it is easily removable. Better upgrade to CPython 3.7+ and use built-in function detection.
I was actually in a situation where I was handling kivy and/or termux in third party code only written for one or the other, so the ability to remove environment variables helped in ton.
I just meant that you're clearly in the right here about environment variables being a poor choice when there's another option. In my use-case, I'm tricking underlying libraries that made that poor choice. It's not actually safe unless I verify that every version of the library used engages the exact environment variables I'm using, document the behavior clearly for users to debug problems with, and file issue reports upstream. The version detection would be of any dependency packages whose unsafe behavior I'm relying on.
@ccpizza what if I removed first answer and left only second?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.