Search code examples
androidpython-3.xterminal-ide

Python 3 on Android using TerminalIDE


I want to code Python 3 on my Android device. So I went through Lanky Cyril's blog post on using the Terminal IDE to put all that I needed to do code on an Android terminal. For Python 2.X that is installed on the blog, it works like a charm.

So I installed Python 3 using the same instructions. This is what I get when I try to start Python 3:

terminal++:~$ ~/python3                                                                                                                                              
Fatal Python error: Py_Initialize: unable to load the file system codec
LookupError: no codec search functions registered: can't find encoding
Segmentation fault 

I made sure:

export PYTHONHOME=/data/data/com.googlecode.python3forandroid/files/python3
export PYTHONPATH=${PYTHONHOME}/lib/python3.2/lib-dynload

I also checked on why python 3 is not loading on StackOverflow.

So the problem could be the python build. Has anyone worked around this?


Solution

  • I worked out the solution by going to the Python-for-Android (Py4A) home and found the script that allows Python3 to run as a "Stand alone" on Android.

    There are 3 ways to do this:

    • I created the "standalone.sh" script, saved it at "HOME"(export HOME=/data/data/com.spartacusrex.spartacuside/files), changed its mode to executable, called it and python appeared.

    • I opened the ~/.bashrc and pasted copied in the code:

    export EXTERNAL_STORAGE=/mnt/sdcard/com.googlecode.python3forandroid
    export PY34A=/data/data/com.googlecode.python3forandroid/files/python3
    export PY4A_EXTRAS=$EXTERNAL_STORAGE/extras
    PYTHONPATH=$EXTERNAL_STORAGE/extras/python3
    PYTHONPATH=${PYTHONPATH}:$PY34A/lib/python3.2/lib-dynload
    export PYTHONPATH
    export TEMP=$EXTERNAL_STORAGE/extras/python3/tmp
    export PYTHON_EGG_CACHE=$TEMP
    export PYTHONHOME=$PY34A
    export LD_LIBRARY_PATH=$PY34A/lib
    $PYTHONHOME/bin/python3 "$@"
    

    Note that this means every time you launch Terminal IDE, you will automatically load Python and find yourself at the Python prompt.

    • To launch Python the normal way, like shown by Lanky Cyril, paste the following code in the .bashrc:
    export EXTERNAL_STORAGE=/mnt/sdcard/com.googlecode.python3forandroid
    export PY34A=/data/data/com.googlecode.python3forandroid/files/python3
    export PY4A_EXTRAS=$EXTERNAL_STORAGE/extras
    PYTHONPATH=$EXTERNAL_STORAGE/extras/python3
    PYTHONPATH=${PYTHONPATH}:$PY34A/lib/python3.2/lib-dynload
    export PYTHONPATH
    export TEMP=$EXTERNAL_STORAGE/extras/python3/tmp
    export PYTHON_EGG_CACHE=$TEMP
    export PYTHONHOME=$PY34A
    export LD_LIBRARY_PATH=$PY34A/lib
    

    You will notice that the last line in the second solution has been taken out and put in an executable file "~/python". Here is the code:

      #!/system/bin/sh   
      /data/data/com.googlecode.python3forandroid/files/python3/bin/python3 "$@"
    

    I used the second one so that when I launch Terminal IDE, I get my Python prompt instantly. I installed the third solution so that, should I leave the Python prompt, I have a way to get back in the same terminal session.