Binoy Babu Binoy Babu - 1 year ago 117
Python Question

Running from within an Android Process

I'm trying to run a python script
from within an Android Process.

Here are the steps I've followed:

  1. I have procured python binaries and need linked libraries.

  2. I have tested them and they are working in the terminal emulator.

  3. I have added them to my asset folder and copied them to the private
    storage and made them executable.

  4. I have set

  5. When that failed I even tried setting

But still I get the following error:

07-19 13:35:15.391 26991-26991/com.vibhinna.example I/System.out: Here is the standard output of the command:
07-19 13:35:32.001 26991-26991/com.vibhinna.example I/System.out: Here is the standard error of the command (if any):
07-19 13:35:32.001 26991-26991/com.vibhinna.example I/System.out: Fatal Python error: Py_Initialize: Unable to get the locale encoding
07-19 13:35:32.001 26991-26991/com.vibhinna.example I/System.out: ImportError: No module named 'encodings'
07-19 13:35:32.001 26991-26991/com.vibhinna.example I/System.out: Current thread 0xb6f0dec8 (most recent call first):

Here is the code used to execute the file.

String pyPath = getFilesDir().getAbsolutePath() + "/usr/bin/python";
String helloPath = getFilesDir().getAbsolutePath() + "/usr/bin/";
ProcessBuilder pb = new ProcessBuilder(pyPath, helloPath);
pb.environment().put("LD_LIBRARY_PATH", "/system/lib:" + getFilesDir().getAbsolutePath() + "/usr/lib");
pb.environment().put("PYTHONHOME", getFilesDir().getAbsolutePath() + "/usr/lib:" + getFilesDir().getAbsolutePath() + "/usr/bin");

Process proc = pb.start();
BufferedReader stdInput = new BufferedReader(new

BufferedReader stdError = new BufferedReader(new

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {

What am I doing wrong? How do I get this to work?

Answer Source

This was pure idiocy. After 5 hours of hair pulling I finally found out what went wrong. I didn't copy the /usr/lib/python3.5 folder to the appropriate Android data folder.

This link was extremely helpful - How does python find packages?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download