1. text

    Python in Terminal IDE on Android.

    I love Terminal IDE. And if you’re a programmer with an Android device, chances are you love it, too. And if you don’t know about it, you should.

    Out of the box, it supports many things, but Python is not one of them. There is Python for Android, but it isn’t available in Terminal IDE by default.

    This article covers the steps needed to set up the most convincing Python-enabled system on your Android device.

    Installing components.

    1. Install Terminal IDE (Google Play link | Official site).
    2. Install Hacker’s Keyboard (Google Play link). You’ll need it to have functional keys, that is, if you’re planning to use a screen keyboard. Terminal IDE comes with its own keyboard, which is just as functional, only ugly. Chuck Norris recommends Hacker’s Keyboard.
    3. Install Python for Android (Google Code link). NB: You have just installed an installer. To actually enjoy Python on your phone, launch it and press “Install.”

    Basic configuration (works with unrooted devices!).

    Now you want Terminal IDE to recognize Python. By default, it indeed doesn’t:
    terminal++@$ python
    bash: python: command not found

    To change this, edit your ~/.bashrc: $ vim ~/.bashrc
    (I have found nano to be a little buggy, not letting me save at times, so I’m opting for vim. Again, if you have never used vim, you really should. There’s plenty of tutorials online.)

    And append this:
    export PYTHONHOME=/data/data/com.googlecode.pythonforandroid/files/python
    export PYTHONPATH=~/py:${PYTHONHOME}/lib/python2.6/lib-dynload

    I keep user-created Python modules in ~/py; if you don’t, notice the second line and modify it accordingly.

    NB: You might want to make sure these are the actual paths on your device:
    $ cd /data/data
    $ cd com.googlecode.pythonforandroid/files/python && ls
    bin lib
    $ ls ./lib
    . . .
    $ ls ./lib/python2.6

    Great; everything is there. If you have installed a different version of python and/or it’s in a different location, edit your ~/.bashrc accodringly.

    One more thing: if your ~/.bashrc lacks a HOME env variable, you might consider adding it. To figure out where your ~ is, do this:
    $ cd ~
    $ pwd

    Then add this to ~/.bashrc:
    export HOME=/data/data/com.spartacusrex.spartacuside/files

    Now that ~/.bashrc is modified, source it to the shell: source ~/.bashrc

    Finally, let’s bring the Python interpreter home: vim ~/python

    Enter the following into the newly created file:
    /data/data/com.googlecode.pythonforandroid/files/python/bin/python "$@"

    And make it executable: $ chmod +x ~/python

    Hooray! If everything is fine, now you should be able to run the python interpreter without problem:
    $ ~/python
    'import site' failed; use -v for traceback
    Python 2.6.2 (r262:71600, Mar 20 2011, 16:54:21)
    [GCC 4.4.3] on linux-armv71
    Type "help", "copyright", "credits" or "license" for more information.

    Well… almost without problem. On both devices that I tried this on (HTC Desire HD running IceColdSandwich and Samsung Galaxy S3 running ParanoidAndroid) I got the error 'import site' failed. Traceback doesn’t shed much light on the problem, so what I did (I know, dirty, as it patches the problem instead of solving it), I created a file site.py in ~/py (remember, the folder that I added to PYTHONPATH; in your case it could be different), and now python imports an empty module “site” and doesn’t throw the error.

    Advanced configuration (requires root!!).

    Now, you probably want Python to actually start from /usr/bin/python or /system/bin/python? Me too. Goes without saying that you’ll need to root your device for that, but let’s be serious here, if you have come this far, you were likely rooted from the start.

    By default, Android, even rooted, mounts / and /system as read-only file systems:
    $ su
    # cd /system/bin
    # mkdir LOL
    mkdir: can't create directory 'LOL': Read-only file system

    This can be overridden, and that’s what we’re going to do:
    # mount -o rw,remount /
    # mount -o rw,remount /system

    NB: Don’t forget to remount them back to read-only, as outlined in the “cleanup” section.

    Now, to the fun part. The weird stuff in the beginning is so that we can return to the home folder of the regular user.
    # exit
    $ cd ~ && su
    # cp ./python /system/bin/python
    # rm ./python

    Does it work?!
    # python
    Python 2.6.2 (r262:71600, Mar 20 2011, 16:54:21)
    [GCC 4.4.3] on linux-armv71
    Type "help", "copyright", "credits" or "license" for more information.

    Now you can create executable python files with #!/system/bin/python in the header, and they are going to work as expected.

    But what if you’re used to Python (and other system executables / interpreters) being in /usr/bin/python? I, for one, am, and many files you and I will work with will expect it to be there. So, let’s go ahead and symlink this shit:
    $ su
    # cd / && mkdir /usr
    # ln -s /system/bin /usr/bin

    Congratulations, executables can be found by their /usr/bin path.

    Cleanup after advanced configuration.

    THIS IS VERY IMPORTANT. We must remount system folders as read-only again.
    # mount -o ro,remount /
    # mount -o ro,remount /system
    # exit


    Hope you found this helpful. If you’re using a screen keyboard and/or a phone instead of a tablet, you might consider taking a look at my upcoming post on configuring Vim to work best with Hacker’s Keyboard, and also on simplifying Python development in Vim.