Running desktop code on Pythonista
My question: What is the best way for Python to detect if it is running in Pythonista vs my computer?
I love using the dropbox module to sync my Pythonista code between devices, and it's nice to edit things from my computer every once in a while.
Many of my scripts serve the same basic function, but I've had to write a Pythonista version and a separate OSX version of the script, sometimes for minor differences such as getting credentials with keychain vs python-keyring.
Because Pythonista seems to ignore my shebang, it seems like it would be pretty easy to get a single script running on both platforms without much effort, at least in some cases.
So far, my best answer is something like:
if sys.platform == 'unknown':
# execute pythonista specific code here, e.g:
# import keychain
# mypass = keychain.get_password()
elif sys.platform == 'darwin':
# execute osx specific code here, e.g:
# import keyring
# mypass = keyring.backends.OS_X.Keyring.get_password()
elif sys.platform == 'linux2':
The only think I don't care for is that sys.platform is 'unknown' -- which might be true for other platforms I haven't explored yet (Android or something).
- import platform; platform.system(); # returns 'Darwin' on both iOS and OSX, won't work
- import os; os.name; # returns posix on both, won't work
Another that might work:
Returns some device-specific information such as the local hostname, which wouldn't be terribly helpful (would work on one iOS device but not the others), but also some kernel information that could be used (but might change as the OS gets updated). E.g. if os.uname() == '12.4.0'
So far, I think the sys.platform == 'unknown' is the best choice I have, but I wanted to see if anyone had other thoughts or options. Thanks for any help, ahead of time.
Man, I really wish we had gfm style codeblocks... indentation not showing up above in the code.
EDIT: Fixed with html and pre. Thanks to ccc. Still wish for the gfm :)
ccc last edited by
I have not done extensive testing on numerous types of machines but you might try:
import platform if platform.system() == 'Darwin': if platform.machine().startswith('iP'): print('You are running on iOS!') else: print('You are running on Mac OS X!') else: print('Please upgrade to a real computer and then press any key to continue...')
Machines starting in 'iP' would include iPhone, iPad, iPod but NOT iMac, iBook, iBook Pro.
mintyfresh190 last edited by
@n8henrie Why don't you just use a <code>try...except</code> to import one of the Pythonista specific modules as a test?
Good thoughts, thanks to both.
DennisNg141 last edited by
print "platform.system() is -"+platform.system()+""
print "platform.machine() is -"+platform.machine()+""