Python on Android [Root]
If you’ve ever wanted to run Python on Android this is the article for you.
Why Use Python on Android?
Because Python is awesome! But seriously the main reason I embedded Python into my custom ROM was that I had a lot of scripts running on startup and as those scripts got larger and more complex they became more difficult to maintain. One day I needed a script that did some work that was quite a bit more complex than before and I needed to crunch some numbers so I finally decided to make the switch. I never changed any of my old shell scripts but I did start only using Python moving forward. Why did I switch? Mainly because I needed the versatility but I also know that I can do the more with less code, it’s much easier to manage, it scales well, and I haven’t looked back since.
Disclaimer: I am not responsible if you damage your Android device(s).
The only thing you need is ADB, a rooted Android device, and a desire to run Python on it! The only CPU architecture this package supports is ARM (Snapdragon, Exynos, etc.), so if you’re running Android on an x86(Intel, AMD) machine this guide won’t work for you. Knowledge of ADB and Python would be nice to have but not required since I’ll explain what we’re doing as we go.
The first thing we will need to do is download a copy of Python. You can download the required files here, it’s roughly 2000 files so I would grab a snack while it downloads. Once you have the files downloaded and extracted we’ll push it to our device.
Push the Files
We will need to push the entire contents of the system folder you downloaded to the system partition of our Android device. In order to execute the binaries, we will also need to grant execute permission.
We need to remount /system as r/w so we can push the binary, you can achieve this with the following command.
Next, we need to push the files to the device, you will need to change the source folder to point to the location that you downloaded the Python files earlier. Since this is a lot of files it may take a minute.
adb push “E:\sourcetree\AndroidPython3\system” /
Finally, to use it we will need to change the permissions to allow execution.
adb shell “chmod +x /system/bin/python3 && chmod +x /system/python3.4.2/bin/*”
The # symbol means you’re in an Android shell with elevated priviledges and the >>> means you’re in the Python interpreter. If you call adb shell and get a $ it means you don’t have elevated permissions.
I’ve marked commands you run in command prompt in bold, commands that are ran in the Android shell as italic and the commands that are ran in the python interpreter as bold and italic.
Python Text-Adventure on Android?
I think the next logical thing to do would be to play a text-based adventure game written in Python! You can download the source from GitHub here. To play the game on our Android device we will first need to choose a place to load the files on Android, I will be using /data/local/tmp however you can place the files anywhere you like.
You can push the files with the following command (Don’t forget to change the local path to wherever you downloaded the files).
adb push “E:\sourcetree\textadventuredemo” /data/local/tmp/
Then you can launch the Android shell with
Finally, you can play the game with the following command
There you have it, you just ran Python code on an Android device. Thanks for taking the time to read this, I hope you found it helpful. If you did please consider applauding, sharing or subscribing for more content. If you have any questions or comments don’t hesitate to drop me a line.
About the author
My name is Aaron Watson and I’m a Software Engineer specializing in embedded Android development based out of Clarenville, Canada. If you’d like to get in touch here is my Email, LinkedIn, GitHub and website.
TextAdventure was created by Al Sweigart.