Binoy Babu Binoy Babu - 4 months ago 22
Python Question

Running hello.py from within an Android Process

I'm trying to run a python script

hello.py
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
    LD_LIBRARY_PATH
    in
    ProcessBuilder
    .

  5. When that failed I even tried setting
    PYTHONPATH
    and
    PYTHONHOME



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/hello.py";
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
InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));

// 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) {
System.out.println(s);
}

// 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) {
System.out.println(s);
}


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

Answer

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?

Comments