I have implemented Chaquopy into my Android app to make use of pre-trained Neural Network models in python.
Trying to call the python code, I am encountering;
“com.chaquo.python.PyException: ModuleNotFoundError: No module named ‘DataLoader’
I am unsure if I have wrongly implemented the file structure for Chaquopy or if there is another reason it cannot import the DataLoader module.
DataLoader.py is in the same location as main.py, inside app/src/main/python/SimpleHRT/ so I don’t see why it cannot access the module.
From the Android app to call the python;
convertImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (! Python.isStarted()){
Python.start(new AndroidPlatform(getActivity()));
}
else{
Python py = Python.getInstance();
PyObject test = py.getModule("SimpleHRT/main");
}
}
});
From the python main.py
from __future__ import division from __future__ import print_function import sys import argparse import cv2 import editdistance from DataLoader import DataLoader, Batch from Model import Model, DecoderType from SamplePreprocessor import preprocess
From the Error log
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.teesside.yellowann, PID: 5735
com.chaquo.python.PyException: ModuleNotFoundError: No module named 'DataLoader'
at <python>.java.chaquopy.import_override(import.pxi:18)
at <python>.java._vendor.six.reraise(six.py:686)
at <python>.java.chaquopy.import_override(import.pxi:59)
at <python>.SimpleHRT/main.<module>(main.py:8)
at <python>.java.android.importer.load_module_impl(importer.py:435)
at <python>.java.android.importer.load_module(importer.py:353)
at <python>.importlib._bootstrap._load_backward_compatible(<frozen importlib._bootstrap>:626)
at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:656)
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:955)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:971)
at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:994)
at <python>.importlib.import_module(__init__.py:126)
at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:154)
at com.chaquo.python.Python.getModule(Native Method)
at com.teesside.yellowann.ImageFragment$3.onClick(ImageFragment.java:173)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 5735 SIG: 9
Application terminated.
EDIT Continuation:
Didn’t want to raise a brand new question when this is pretty much the same thing.
Trying to now implement:
Android Java call
convertImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (! Python.isStarted()){
Python.start(new AndroidPlatform(getActivity()));
}
else{
Python py = Python.getInstance();
PyObject test = py.getModule("SimpleHRT.main").get("main");
PyObject test2 = test.call();
}
}
});
Python Filepaths
class FilePaths:
"filenames and paths to data"
fnCharList = '../model/charList.txt'
fnAccuracy = '../model/accuracy.txt'
fnTrain = '../data/'
fnInfer = '../data/test.png'
fnCorpus = '../data/corpus.txt'
Python call
model = Model(open(FilePaths.fnCharList).read(), decoderType, mustRestore=True) infer(model, FilePaths.fnInfer)
Error log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.teesside.yellowann, PID: 9330
com.chaquo.python.PyException: FileNotFoundError: [Errno 2] No such file or directory: 'SimpleHRT/../model/charList.txt'
at <python>.SimpleHRT.main.main(main.py:138)
at <python>.chaquopy_java.call(chaquopy_java.pyx:283)
at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callThrows(chaquopy_java.pyx:243)
at com.chaquo.python.PyObject.callThrows(Native Method)
at com.chaquo.python.PyObject.call(PyObject.java:190)
at com.teesside.yellowann.ImageFragment$3.onClick(ImageFragment.java:176)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 9330 SIG: 9
Application terminated.
I have tried “./model/charList.txt”, “SimpleHRT/model/charList.txt”, “SimpleHRT/../model/charList.txt”, “…/model/charList.txt”, “./../model/charList.txt”
The base python is in “src/main/python/SimpleHRT/” whereas the documents I need to read from are in “src/main/python/SimpleHRT/model/”
I’m assuming there’s just some nuance about the file-pathing that I’m not grasping…
Advertisement
Answer
It looks like you’re trying to do an implicit relative import. This would work in Python 2, but Python 3 requires you to explcitly indicate when you’re loading a module from the same package.
So instead of from DataLoader, you’ll need to use from .DataLoader or from SimpleHRT.DataLoader.
Alternatively, you could move all the code to the top-level src/main/python directory, and then the import statements would work without any change.
(This may not be related, but you should also use Python module name syntax when callling getModule, i.e. SimpleHRT.main, not SimpleHRT/main. Actually, I’m surprised the second one worked at all.)
EDIT for second part of question: To load resource files packaged alongside your code, see https://chaquo.com/chaquopy/doc/current/android.html#resource-files.