Python in Terminal IDE for Android

I wanted to see if I could get access to the python interpreter used by SL4A in Terminal IDE so that I could, obviously, use a terminal environment to create and run scripts since then I can use vim, and I can use tmux to give me a decent working environment with my tablet in landscape mode. I googled around for awhile and didn’t manage to find anything explicit on how to set this up, and then figured out how to use google and found this blog post which links to a script from the p4a (python for android) repository. I’ve reduced the script a bit (shown below), removing the egg cache and adding a constant for the base path. I’ve also not bothered exporting the external storage environment variable. If you want these things, see the original script mentioned previously :)

Please note the below takes into account just using python with Terminal IDE, and I’ve not linked anything to /system/bin because not everyone who has Terminal IDE installed will want to have root, so our python script is kept in Terminal IDE’s home, which in turn is added to the path in .bashrc.

So I created the below file as ~/python:

#!/system/bin/sh
BASE=/data/data/com.googlecode.pythonforandroid/files/python
export PYTHONPATH=${PYTHONPATH}:${BASE}/lib/python2.6/lib-dynload
export PYTHONHOME=${BASE}
export LD_LIBRARY_PATH=${BASE}/lib
${BASE}/bin/python "$@"

Then made it executable and added ~/ to my $PATH in ~/.bashrc:

terminal++@127.0.0.1:~$ chmod +x ~/python
terminal++@127.0.0.1:~$ echo export PATH=$PATH:~/ >> ~/.bashrc

And now when you restart Terminal IDE, you should be able to type ‘python’ to get the interactive python interpreter!

Leave a Reply