Tuesday, January 29, 2013

Python for Android

I installed SL4A (Scripting Layer for Android) and Python for Android on my new Android Phone last night, and I've got to say this makes my phone the neatest portable device I've had since my HP 200LX.

The 200LX was basically an IBM PC-XT that fit in your pocket. It was a clamshell that ran a ROM version of DOS that was even capable of running early versions of Windows if you really, really wanted. My interest was more in running full DOS IDE packages like Turbo Pascal, Modula-2, Turbo C/C++, FORTH, etc. It also had a spreadsheet (Lotus 1-2-3) on ROM, ran dBase and the Clipper compiler well, and a host of other things. I kept one with me for over 10 years, long past its day, because I couldn't find anything comparable that I could carry with me as well and do so much with. There's nothing like being able to whip out a machine with a decent compiler on it while your wife is in the fitting rooms at the mall.

Well now I've got Python on my Android phone. This was my second try.

A False Start
Initially, I did a search on the Play Store. There I found something called QTPython+ that was a free download with at least a couple of positive reviews. Well, I couldn't figure out what it was up to. Its programs don't use normal Python I/O, and so far as I could tell (following the in-app link to the dev website), it's not that well documented. I'm clearly missing something, but it didn't work out for me.

Then, while paging idly through a copy of Android Development with Eclipse, I saw a mention in a later chapter about Python and other scripting languages. I got my phone and went out to the SL4A site right away, book in one hand, Android phone in the other.

The SL4A Install
For those that haven't been through it yet, it's not difficult to do the install, but it's not what you'll be used to from Google Play, or the Amazon App Store, either. At the SL4A site, you'll download and install the latest version of SL4A, which more or less is the support layer for the scripting language. I downloaded sl4a_r6.apk, which would be the R6 version of it. By itself, it doesn't get you much. I think you can get a command line with it.

The next step is to get the Python installer from the Python for Android site. I pulled one marked for R6, Python_for_Android_R6.apk. This is Python 2.6.2, there's another package for Python 3.

Either way, when you install that and start it up you'll get a menu asking whether you want to "Install". This seems confusing (at least it did to me late last night) as you'll ask yourself "Isn't that what I just did?" Actually, you've installed the installer. Click install and wait a few moments while it goes out and gets the other packages you need for a working Python install.

After that, you'll start the app from the SL4A icon. There you'll see a list of the example Python modules the bundle comes with. There are some good examples of starter Android apps there. But you don't have to do things the "Android Way" if you don't wish...

Command Line Junkie
You can also edit your own command-line/text-console Python programs. My first go was an off-the-top-of-my-head version of Guess.py from Invent Your Own Computer Games with Python (the textbook I'm using to start students off with Python in class this year.)

It had to be modded a bit for Python 2, of course, but it worked like a charm.

What fun!

The Source Code (as if you care) ;)
from random import randint
print 'I am thinking of a number between 1 and 20.'
print 'Try to guess it in 6 guesses or less.'

guesses = 0
guess = 0

while guesses<6:
    print 'Your guess? ',
    guess = int(raw_input())
    guesses = guesses + 1

    if guessnum:
        print 'Too high.'

    if guess==num:

if guess==num:
    print 'You got it in ',
    print guesses,
    print ' guesses!'

if guess!=num:
    print 'Too bad, the number was ',
    print num


  1. how did you mod it to make it python 2? specifically 2.7.3?

    1. You can install either Python 2 or Python 3 using SL4A. I'm using Python 2 on my phone, and Python 3 on my tablet. Makes no nevermind. ;)