Can't start python script in Android Studio via Chaquopy after including dateparser

Viewed 715

I'm trying to start my python scripts via chaquopy plugin in Android studios with kotlin.

This is the way i'm starting the python script via Kotlin

    private fun getPythonHello(): String {
        if (! Python.isStarted()) {
            Python.start(AndroidPlatform(this));
        }
        val python = Python.getInstance()
        val pythonFile = python.getModule("edit_package_data_to_application")
        return pythonFile.callAttr("main").toString()
    }

During the debugging i already included all required libraries (nltk, scitkit and others) via pip in the gradle(app) file. After includuing all libraries this error appears: "AttributeError: 'zipimporter' object has no attribute 'exec_module'"

Does anybody know why this error can appear?

2020-08-25 16:41:32.538 12129-12129/com.pro.useyournotes E/ExceptionTag: com.chaquo.python.PyException: AttributeError: 'zipimporter' object has no attribute 'exec_module'
    com.chaquo.python.PyException: AttributeError: 'zipimporter' object has no attribute 'exec_module'
        at <python>.dateparser.utils.strptime.patch_strptime(strptime.py:40)
        at <python>.dateparser.utils.strptime.<module>(strptime.py:69)
        at <python>.java.chaquopy.import_override(import.pxi:26)
        at <python>.dateparser.parser.<module>(parser.py:12)
        at <python>.java.chaquopy.import_override(import.pxi:26)
        at <python>.dateparser.date_parser.<module>(date_parser.py:11)
        at <python>.java.chaquopy.import_override(import.pxi:26)
        at <python>.dateparser.date.<module>(date.py:12)
        at <python>.java.chaquopy.import_override(import.pxi:26)
        at <python>.dateparser.<module>(__init__.py:4)
        at <python>.java.chaquopy.import_override(import.pxi:26)
        at <python>.functionalities_pre_processing.<module>(functionalities_pre_processing.py:22)
        at <python>.java.chaquopy.import_override(import.pxi:26)
        at <python>.main_useyournotes_analysis.<module>(main_useyournotes_analysis.py:7)
        at <python>.java.chaquopy.import_override(import.pxi:26)
        at <python>.edit_package_data_to_application.<module>(edit_package_data_to_application.py:14)
        at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
        at <python>.importlib._bootstrap_external.exec_module(<frozen importlib._bootstrap_external>:783)
        at <python>.java.android.importer.exec_module(importer.py:477)
        at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:671)
        at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:975)
        at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
        at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
        at <python>.importlib.import_module(__init__.py:127)
        at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:153)
        at com.chaquo.python.Python.getModule(Native Method)
        at com.pro.useyournotes.MainActivity.getPythonHello(MainActivity.kt:69)
        at com.pro.useyournotes.MainActivity.onCreate(MainActivity.kt:59)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        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)
1 Answers

It looks like dateparser assumes that standard library modules are loaded by a loader that supports the new importer API. Unfortunately, Chaquopy loads the standard library using zipimporter, which despite being a part of the standard library itself, still doesn't implement the new API.

This issue is fixed in Chaquopy 9.0.0. With older versions, you can work around it by running the following code before importing dateparser:

from zipimport import zipimporter

def create_module(self, spec):
    return None
zipimporter.create_module = create_module

def exec_module(self, module):
    exec(self.get_code(module.__name__), module.__dict__)
zipimporter.exec_module = exec_module

I also removed geograpy, because it was also an issue. Another library i had to remove was googleapiclient, now i will transfer the data on android side and will work with it there.

geograpy appears to be an abandoned library which isn't compatible with Python 3. It has some more recent forks which you could try.

As for googleapiclient, I'm not aware of any problems with that package, so please give details in a separate question, or preferably, create an issue on our GitHub page.

Related