How to run and schedule Python scripts on iOS
So you automated something with a Python script - perhaps checking if your favourite food is available - and maybe you scheduled it to run on your PC every day.
But maybe there are days when you are not using your computer, but still wanted to run this script. Wouldn't it be cool if you could do this on your iPhone?
Well, the good news is: you can.
Even better news: it is also possible to semi-automate them by location or time triggers.
The app you need to run Python scripts on iOS
There are a few alterntives, the one I recommend is Pythonista 3. It's not the cheapest, but it's by far the most useful and feature-rich application. Since it's a universal app you only need to buy it once, and you can edit/run scripts across all your i-devices though iCloud file sharing.
Furthermore, through a neat shell extension called stash, you can install any 3rd party module via pip.
Uploading and running scripts
There are various ways you can open or upload a file to Pythonista. On the leftmost pane:
- you can choose any file from your iCloud library
- use the Open button under External files section to open files from other apps / locations
- use the + button in the bottom left corner to create a new blank script file
- here you can write your own code
- or paste in code from another file
Running the script is as easy as clicking the Play button in the header of the script pane.
The app you need for scheduling
Launch Center Pro is an app that you can use to perform tasks based on location and time triggers.
The neat thing is that it can also perform actions using iOS's URL scheme. This means it can open Pythonista, and run any script within it!
Here's how it works:
- Create a new action by tapping the pencil button in the top right corner, and clicking on an empty slot. Then in the URL field use the following logic:
pythonista://script.py?action=run
This will run script.py that is located in your Pythonista's local root folder. - If you're script is in a folder, then use this scheme:
pythonista://Folder/script.py?action=run
or
pythonista://Folder/Folder2/script.py?action=run
- If you want to run a script from you iCloud library, use this scheme:
pythonista://Folder/script.py?action=run&root=icloud
Once created, tapping this action will do exactly as you'd expect: it will open Pythonista, and instantly run the defined script.
Scheduling
It's possible to setup time and location triggers in Launch Center. When creating or editing an action, use the Schedule and Location triggers menus. It is pretty self-explenatory how to use them. This way you can schedule scripts to run at a certain time of a weekday, or when you arrive to work, leave home, etc.
Please note that Launch will not be able to perform any action in the background without your permission. It will display a notification on the defined time / location, but it will only execute the action once you tap on the notification and unlock your phone.
Still, this is a pretty neat way to scedule scripts from your iPhone.
Final words
The options detailed in this article are not free. If you buy both Pythonista and Launch Center Pro it will cost you $15 [Update: since I wrote this article Launch Center Pro went freemium. You only have to pay $9.99 for Pythonista]. But if you only use simple scripts, it could be cheaper on the long run than scheduling them on web services like AWS or Digital Ocean.
Plus you will get a very capable Python editor, that you can use on the go to experiment. It happend to me more than once that an idea came to my mind during my daily commute and I could test it straight away on my phone via Pythonista. It is a really powerful tool and super easy to use.
is there a way to safely use dotenv() ? any recommendations?
It sounds like this approach will allow me to send myself a daily news summary ran via python script :)
I only had to pay $9.99. No cost for the Launch Center Pro (so far?). I gave it a try and executed my script from the Launch Center Pro as per the instructions above, with the default location for the script. Next i’ll try scheduling !!
Yeah, in the meantime Launch Center Pro went to freemium model. I should update the article :)