Forum Archive

Best Practices: Recognize when Pythonista is calling a script

holyjaw204

Hi all,
I'm looking for a best practice way to recognize when Pythonista is calling a script (vs. being called from, say, Alfred, or just in a plain old terminal emulator).

The best I can find is to try to import a Pythonista-only module, and work from there, but it seems pretty hacky. I looked around the documentation and didn't find anything.

Any ideas?

omz

If your script actually uses the Pythonista-only module when running there, but has an alternative code path for running on a Mac/PC (typical example: using clipboard), I don't think it's such a bad idea to try importing the module and falling back to something else if you get an ImportError.

On the other hand, if you're just interested in knowing where the script runs, without the code actually being different, you could for example use platform.machine().

holyjaw204

@omz – Ah, great, I like that! So I can tell if pythonista the application is running it by try/catching an import for a Pythonista specific module, and I can also check the hardware with platform.machine(). Thanks for the solution.

ccc

Also see: http://omz-forums.appspot.com/pythonista/post/4691117899513856