Search code examples
pythonandroid-studiochaquopy

Insert value to function in python file using Chaquopy in Android Studio


I am trying to insert 'Hello World' to def m(input) in python file. But it is not working how can I solve that?

main.py

def m(input):
    return input

MainActivity.java

if(!Python.isStarted())
    Python.start(new AndroidPlatform(this));

Python py = Python.getInstance();
PyObject pyf = py.getModule("main");
PyObject obj = pyf.callAttr("m('Hello World')");
text.setText(obj.toString());

This is the error I got

com.chaquo.python.PyException: AttributeError: module 'main' has no attribute 'm('Hello World')'

Solution

  • Arguments to callAttr should be passed separately, not as a single Python expression. For example:

    pyf.callAttr("m", "Hello world");
    

    For more information, see the callAttr documentation and the examples.