Run Python Scripts In Android

Run Python Scripts In Android

The necessary environment settings required to run python from a shell script can be found here:

To use, make the script executable and run from the shell:

chmod a+x

or, if that doesn’t work (and it may not, depending on filesystem) just:

sh /sdcard/

The script at time of writing looks like this:

#! /bin/sh

export EXTERNAL_STORAGE=/mnt/storage
export TEMP=/mnt/storage/com.googlecode.pythonforandroid/extras/python/tmp
export PYTHONHOME=/data/data/com.googlecode.pythonforandroid/files/python
export LD_LIBRARY_PATH=/data/data/com.googlecode.pythonforandroid/files/python/lib
/data/data/com.googlecode.pythonforandroid/files/python/bin/python "[email protected]"

Note that you can pass arguments to python, ie:

./ hello.Py

Why is it so complicated?

Android is quite fussy about what it lets you have access to. Executables and shared libraries have to be on the main file system (typically /data/data somewhere)

The only writable parts of /data/data are the ones allocated to your package (apk) (ie: /data/data/com.googlecode.pythonforandroid ) although you can use chmod to override this.

Bulky items should live on the /sdcard, (not always an actual sdcard). Android allocates a package based spot to store this stuff (ie, /sdcard/com.googlecode.pythongorandroid/extras). For most android devices, space in the main fs is at a premium. Thus the extensive library files are places on the /sdcard.

Python needs to be told where all these non-standard places are. (Thus, PYTHONHOME etc)

source :-

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button