Home » Android » java – Getting error "can't create package cache dir, 'cachedir/packages'" when trying to use Jython in Android-Exceptionshub

java – Getting error "can't create package cache dir, 'cachedir/packages'" when trying to use Jython in Android-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am using org.python:jython-standalone:2.7.1 to run Python code inside Android application, as reference to this question, but I am getting an error

can’t create package cache dir, ‘cachedir/packages’

I am using gradle for installing Jython Standalone dependency

implementation ‘org.python:jython-standalone:2.7.1’

I also provided the Read\Write permission as I thought it could be helpful

Here is my code for Interpreted example

import org.python.core.PyInstance;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class InterpreterExample {
    public PythonInterpreter interpreter = null;

    public InterpreterExample(){
        PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
        this.interpreter = new PythonInterpreter();
    }

    void execfile(final String filename){
        this.interpreter.execfile(filename);
    }

    PyObject createFunction(final String functionName){
        return this.interpreter.get(functionName);
    }

    PyInstance createClass(final String className, final String opts){
        return (PyInstance)this.interpreter.eval(className + "(" + opts + ")");
    }
}

and I am using this class as

public void runScript(View view){
    InterpreterExample ie = new InterpreterExample();
    ie.execfile("hello.py");

    PyObject result = ie.createFunction("test").__call__();
    String strResult = (String) result.__tojava__(String.class);

    Toast.makeText(this, strResult, Toast.LENGTH_SHORT).show();
}

need a solution for this error,
Thank You

How to&Answers: