Hey, there today I’m writing about launching or using Python from the shell on Android. Or more precisely running scripts from a shell on Android. You might sometimes need to use Python via shell or prefer to use it via shell. So in this tutorial, we will do just that.
If you haven’t already installed Python on your android phone then click the link provided a few words back. I mean being able to run scripts from any directory directly via shell is far more efficient than copying all of the scripts to a specified folder or directory.
Without further ado, I would like to start the tutorial by providing the link to the required materials for this tutorial.
Compress files online before downloading You might wanna read.
- Download Python.sh
This is a fairly simple tutorial. You don’t necessarily need root access for this tutorial to work but having root access makes things a little bit sweeter. We will make use of a script to use or launch Python via shell. And of course, also run the Python scripts via shell.
1. Launch Python via shell on android
How to connect to WI-FI without any password interesting read.
Assuming you have already installed Python on your android phone. While installing Python on your android device you had installed 2 applications on your device during the process. We will make use of the SL4A application the one you had installed earlier throughout this tutorial.
- Okay, first of all, launch the SL4A application then access the menu options. To access the menu option you might need to long press the menu key or just touch it once. It depends on the particular device. In my case, I had to just touch the menu key just once.
- after that click on “view” option you see.
- Next up click the one that says “Interpreters” and then click on “shell” option you see at the top.
- Now a shell session will be launched. By default it looks like crap, you might wanna increase the font size. To increase the font size again access the menu and click on “Preferences” option. Scroll down and click on “Font size (pt)” option under “Terminal” header. And set it to whatever fits best for you, I set it to 17. I device’s screen size is 5 inches.
- Store the recently downloaded “Python.sh” file in the “sl4a” folder which should be present in the root directory of your internal storage or external storage. Okay now back to the shell session. type in “sh python.sh” in the shell and press enter or okay. Voila, python will be launched.
2. Run python scripts via shell
Old vs new method WPS WPA tester wifi hack Popular post.
It is easy to run scripts from the shell. Type in “ls” command and you should see a list of files and folders present in the current directory i.e ‘sl4a’. you should see a directory named scripts in the list. Yes? okay then perfect.
- Eg: type in this command “sh python.sh scripts/hello_world.py” you should get the output ‘Hello world!’. If you get this output then you know everything is fine and working.
let me explain the use of command used above. “sh python.sh” command will always be constant only the later part will be different according to the situation. Syntax: “sh python.sh path-to-the-directory-where-script-is-located”.
Your shell session should always be in the same directory where the “python.sh” file is available for this command to work.
3. The small mini tutorial only for rooted phones
Remember the talk about things being a little bit sweeter if your device is rooted? If you have a rooted device then you can copy the “python.sh” script file to the /system/bin directory and launch Python from any directory.
I mean you won’t need to be in the same directory where the “Python.sh” is located to launch Python or run Python scripts. You can launch Python scripts from any directory you prefer or are currently in.
Here is a short tutorial on how to copy “Python.sh” to the /system/bin directory using ES file explorer:
- Launch ES file explorer and open menu options and scroll down till you see “Root Explorer” option and click on it.
- After that click on second last option that says “Mount R/W” and tick the mark under “RW” header next to “/system” and press ok.
- Again open menu options and click on “Device” option and navigate to “system/bin” directory.
- Finally, paste the “Python.sh” file in there and rename it to whatever you want. I named it “py” just “py” not “py.sh”
Now you can go back to your shell session and launch Python or run scripts from any directory you want.
Note: To launch or provoke Python run scripts now you need to type in whatever you renamed the “Python.sh” file to in /system/bin folder.
I hope this tutorial was helpful to you and saved you some time. Thanks for reading all the way through.
Have a good day.