Hello all together,
I have a python and android related issue. I hope that this is no
problem because apart from stack overflow, where I also posted this
question (but got no answer so far), I think a python mailing-list is
the best place to post this question.
I am trying to use Python-Scripts in Android. So far a managed to get
python as a shared library and linked it to my android project using
Android.mk. I also created a file named wrapper.c which gets called from
Java using jni and is used to call the python-script using the
Python-C-Api. But when I try to open the script using
PyImport_ImportModul, it always returns NULL.
First here is my Android.mk file which I put in the jni-folder of my
Android project:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := luajit
LOCAL_SRC_FILES := libluajit.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := luajit
LOCAL_SHARED_LIBRARIES += libcutils
LOCAL_LDLIBS := -llog
LOCAL_MODULE := wrapper
LOCAL_SRC_FILES := wrapper.c
LOCAL_EXPORT_C_INCLUDES := .
include $(BUILD_SHARED_LIBRARY)
This file does not cause any problems so far. wrapper.c is the c-file
which is called from java using jni. This file uses the Python-C-Api to
call a Python script. This is wrapper.c:
jstring
Java_com_example_pythonandroid_MainActivity_stringFromJNI(JNIEnv* env
jobject thiz, jstring filename, jstring path) {
PyObject *modul, *funk, *ret;
char *erg;
const char *filenameCString = (*env)->GetStringUTFChars(env,
filename, 0);
char *pathCString = (*env)->GetStringUTFChars(env, path, 0);
__android_log_print(ANDROID_LOG_INFO, "CFilename", filenameCString);
__android_log_print(ANDROID_LOG_INFO, "CPath", pathCString);
Py_Initialize();
PySys_SetPath(pathCString);
modul = PyImport_ImportModule(filenameCString);
if(modul!=NULL) {
funk = PyObject_GetAttrString(modul, "helloPy");
ret = PyObject_CallObject(funk, NULL);
erg = PyString_AsString(ret);
__android_log_print(ANDROID_LOG_INFO, "pyAnswer", erg);
Py_DECREF(ret);
Py_DECREF(funk);
Py_DECREF(modul);
Py_Finalize();
return (*env)->NewStringUTF(env, erg);
} else {
return (*env)->NewStringUTF(env, "Error: Modul not found");
}
}
Filename and Path (set on the java-site) in this case are:
/data/data/com.example.pythonandroid/files/test.py
/data/data/com.example.pythonandroid/files
The file test.py was placed in the res/raw folder of the Android Project.
To check if they refer to the correct file, I printed the file
/data/data/com.example.pythonandroid/files/test.py in wrapper.c and got
the content of my python script. So everything seems to be alright on
this end.
From the information I've read so far I understand that I have to set
PySys_SetPath to the path where Python should look for modules. After
that I can import them which PyImport_ImportModule. I also tried to set
PySys_SetPath(".") even though it seems unrealistic that the actual path
and the path of the script are the same. Anyway: it didn't work.
I hope I gave you all the information you need. Thanks in advance.
greetings
Dominik
--
https://mail.python.org/mailman/listinfo/python-list