Friday 11 May 2012

How to Run Your Own Python Scripts on an Android Device

While I don't usually post my programming activities on this blog, I have been dedicating increasing amounts of time dedicated to it. Especially now that I have just acquired my first android phone (sayonara iPhone) I have been wanting to run my own scripts on it. Now since I don't have much (any) experience programming java, the native programming language for android, I decided to set about figuring out how to run scripts in a language that I do know, hence python for android. Android app development is possible with just your home computer with some free software and you are free to run it on your device without a jailbreak that might void your warranty (unlike another popular smart phone).

Firstly, I will show you how to set up your android device to run Python scripts using a google product called Scripting Language 4 Android (SL4A). This is fairly straightforward.
Firstly, you need to make sure that you allow your phone to install software from unknown sources (in settings). Then, in your android internet browser, navigate to code.google.com/p/android-scripting. Then click on the QR code (black and white box) and proceed with downloading and installing SL4A.
Next, you are going to want to go back to the page and on the left hand side under featured>downloads, you'll want to download and install PythonForAndroid_r4.apk (latest at time of writing). Once the download of this is complete DO NOT CLICK UNINSTALL, it's easy to do, that's why I'm warning you.

Where to click in the SL4A download site


OK, so now the phone is set up to run python scripts and if you now navigate to SL4A (comes up just like a freshly downloaded app should), as in the image you should be able to see all of the pre-loaded python scripts.

SL4A and Python for Android icons
important Python scripts
In the image I have highlighted two scripts in particular. The first is the Hello World app, very straightforward, it will just print "Hello World!" in your notifications bar. The second is more interesting, test.py will show you a run through of features available to you as an Android Python programmer. It is definitely worthwhile to run this app and study the code carefully.

It doesn't seem to want to let me take a screen capture of it, but when you click on one of the scripts to run it, there are five icons that appear and the functions of them are as follows (from right to left): Run python script in terminal, Run python script, Edit python script, Rename python script and, finally, Delete the script.

OK, that's all well and good for running Python apps on the device, but for development the inbuilt script editor probably isn't the best way of doing it (unless you enjoy coding on a tiny screen and you own a bluetooth keyboard and mouse). So, for editing and quickly testing your scripts you are going to want to set up the android SDK and start up an emulator. I am not going to get into setting up the android SDK on this tutorial as it's different between operating systems and I'll be here all day. I may do in the future but for now I will only say it's a case of downloading eclipse from www.eclipse.org and the android SDK from http://developer.android.com/sdk/index.html. However, if you don't know what you're doing just do a google search for "set up android SDK on (insert operating system here)". There are loads of tutorials on blogs and youtube and developer.android.com itself.

Now, once you have the Android SDK set up and eclipse is running, you're going to need to set up an android emulator and do what is described above for your handset on the emulator (and bear in mind that the emulators run much more slowly than your device should). To do this, open eclipse (once the SDK is set up) and click window(eclipse on mac)>AVD manager. Then click New... and you should be on the setup page for an Android Virtual Device. Name it whatever you like and under target choose whichever android OS version you desire. Set storage to whichever value you like also (over 9MiB) and click Create AVD. That's it! once that is finished click start while the AVD of your choosing is highlighted and you will have your very own android emulator running on your system.

Now that an Android Virtual Device is set up and running on your system, you are going to need to install SL4A and python (same as described above but in the emulator) and you are also going to have to know how to transfer python scripts for testing out your applications (this is where I cant help windows and mac users, but you can do a search on it, or upload the python script to a site or email and download it to the emulator). Now, when setting up the SDK you downloaded a directory where it lives, right? Inside this directory (android-sdk-linux on my machine) there is a folder called 'platform-tools' and the tool we are looking for is called 'adb'. This tool will allow us to push files to the android emulator, and here's how:

First, copy your python script to the android SDK directory.
Then use the command:

$ tools/adb push (your file).py /sdcard/sl4a/scripts

to push the file to your emulator.
Once this is complete, open SL4A and you should now see your Python script sitting there ready to be run. Congratulations, you are running your own python script on android! Nice.

One more thing, always be sure to send your scripts (whether on the emulator or your handset) to sdcard/sl4a/scripts. If you don't, you wont be able to run them from sl4a. Thank you, goodnight.







36 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks, Thomas! This article made it really easy to install Python on my Android phone. Awesome post :)

    ReplyDelete
  3. Very interesting, I like the way you write in PC and run in the device with that adb comand, very good, thanks...

    ReplyDelete
  4. Hey Thomas, I followed the same methodology on the physical device, also I can see all the preloaded, ".py" scripts, but while running the script,it just show a black screen no output for hello program.

    ReplyDelete
    Replies
    1. Did you save it in the sl4a/scripts folder?
      I still have sl4a running on my phone, but it has been a good few years since I wrote this so maybe things have moved on.
      Try running one of the notification or built in scripts.

      Delete
  5. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.

    Python Training in Chennai | Python Training Institutes in Chennai

    ReplyDelete
  6. Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  7. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    Data Science Training in Chennai
    Data Science course in anna nagar
    Data Science course in chennai
    Data science course in Bangalore
    Data Science course in marathahalli

    ReplyDelete
  8. Very good information. Its very useful for me. We need learn from real time examples and for this we choose good training institute, we need to learn from experts . So we make use of demo classes . Recently we tried azure demo class of Apponix Technologies

    ReplyDelete
  9. Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing.
    python training in bangalore

    ReplyDelete
  10. Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.

    ReplyDelete
  11. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab Python classes in pune your rss feed to stay informed of any updates.

    ReplyDelete
  12. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading Python classes in pune new articles. Keep up the good work!

    ReplyDelete
  13. Very Interesting Post and thanks for it.I enjoyed reading your articles.You have clearly explained each and every modules in more informative manner for all. Keep it up!!
    machine learning course bangalore

    ReplyDelete
  14. Really very informative post you shared here.It's a great skill to make even the person who doesn't know about the subject could able to understand the subject .Keep it up!! Machine Learning Training in Bangalore

    ReplyDelete
  15. Nice Blog, When i was read this blog i learnt new things & its truly have well stuff y, Thank you for sharing this blog!! machine learning course bangalore

    ReplyDelete
  16. Attend The Data Science Courses Bangalore From ExcelR. Practical Data Science Courses Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Courses Bangalore.
    ExcelR Data Science Courses Bangalore
    Data Science Interview Questions
    ExcelR Business Analytics Course
    ExcelR Data Analytics Courses

    ReplyDelete
  17. I blog often and I really appreciate your information. Your article has really peaked my interest. I am going to bookmark your website and keep checking for new information about once a week. I opted in for your RSS feed too.

    ReplyDelete
  18. I blog often and I really appreciate your information. Your article has really peaked my interest. I am going to bookmark your website and keep checking for new information about once a week. I opted in for your RSS feed too.

    ReplyDelete
  19. "I am looking for and I love to post a comment thatExcelR Online data science training
    ""The content of your post is awesome"" Great work!"

    ReplyDelete
  20. If you're interested in getting a job in Python, look at the advanced Python training offered at Hyderabad from AI Patasala. Python Training in Hyderabad by trained experts at AI Patasala training centre is the best option.
    Python Institute in Hyderabad

    ReplyDelete
  21. Wow, happy to see this awesome post. I hope this think help any newbie for their awesome work. By the way thanks for share this awesomeness from business analytics course in mysore

    ReplyDelete
  22. I appreciate you sharing your experience with using Python scripts on Android. SL4A appears to be a useful technique for bridging linguistic gaps. exciting opportunities lie ahead!"
    Data Analytics Courses in India

    ReplyDelete
  23. Good Blog, I gained new knowledge from reading this blog, which has a lot of useful information. I appreciate you sharing this blog.
    Data Analytics Courses in Agra

    ReplyDelete
  24. Hello Blogger,
    This article provides a valuable guide for running Python scripts on Android devices. Clear instructions and tips make it an excellent resource for both beginners and experienced programmers. Great job.
    Is iim skills fake?

    ReplyDelete
  25. This article effectively demystifies the process of running Python scripts on an Android device, offering a user-friendly approach for implementing Python-based solutions on mobile
    Digital Marketing Courses in Hamburg

    ReplyDelete
  26. This is a wonderfully engaging article. I was searching for this type of information and found it quite enjoyable to read. Please continue to publish more. Appreciate you sharing this.
    daa Analytics courses in leeds

    ReplyDelete
  27. Thanks for this informative guide! I've always wanted to run my Python scripts on my Android device, and your step-by-step instructions made it a breeze.
    Also Read : Deep Learning with Python and TensorFlow/Keras-

    ReplyDelete
  28. Thank you for sharing knowledgeable and excellent tutorial on How to Run Your Own Python Scripts on an Android Device.
    Adwords marketing

    ReplyDelete
  29. The blog post provides great and excellent tutorial on How to Run Your Own Python Scripts on an Android Device.
    Investment banking training Programs

    ReplyDelete
  30. Fantastic tutorial! Your step-by-step guide made Python on Android a breeze. Grateful for the insights. Thanks a ton for sharing!

    Investment Banking Industry

    ReplyDelete
  31. Thanks for this really useful blog post, just what I needed to complete my Python assignment.
    Investment banking analyst jobs

    ReplyDelete