Skip to content
Advertisement

Chaquopy in Android Studio module not found

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;

JavaScript

From the python main.py

JavaScript

From the Error log

JavaScript

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

JavaScript

Python Filepaths

JavaScript

Python call

JavaScript

Error log

JavaScript

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement