Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59188

Python and Android: PyImport_ImportModule always returns NULL

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <djweinhold@web.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.026
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'subject:Python': 0.06; 'filename': 0.09; 'path)': 0.09; 'skip:$ 20': 0.09; 'wrapper': 0.09; 'python': 0.11; 'question.': 0.14; 'posted': 0.15; 'filename,': 0.16; 'mailing-list': 0.16; 'message-id:@web.de': 0.16; 'modul': 0.16; 'modules.': 0.16; 'null);': 0.16; 'skip:j 60': 0.16; 'folder': 0.16; 'library': 0.18; 'trying': 0.19; '(but': 0.19; 'advance.': 0.19; 'file,': 0.19; 'stack': 0.19; 'seems': 0.21; 'import': 0.22; 'issue.': 0.22; 'header:User- Agent:1': 0.23; 'char': 0.24; 'script.': 0.24; 'java': 0.24; 'question': 0.24; "i've": 0.25; 'script': 0.25; 'post': 0.26; 'gets': 0.27; 'tried': 0.27; 'correct': 0.29; 'skip:p 30': 0.29; '0);': 0.29; 'related': 0.29; 'skip:( 20': 0.30; 'work.': 0.31; 'project:': 0.31; 'file': 0.32; 'open': 0.33; 'are:': 0.33; 'actual': 0.34; 'problem': 0.35; 'created': 0.35; 'but': 0.35; "didn't": 0.36; 'thanks': 0.36; 'should': 0.36; 'received:192.168.2': 0.37; 'project': 0.37; 'problems': 0.38; 'same.': 0.38; 'to:addr:python-list': 0.38; 'skip:_ 30': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'even': 0.60; 'read': 0.60; 'hope': 0.61; 'skip:* 10': 0.61; 'first': 0.61; 'information': 0.63; 'refer': 0.63; 'linked': 0.65; 'here': 0.66; 'received:212.227.17': 0.68; 'received:web.de': 0.68; 'greetings': 0.72; 'apart': 0.72; 'skip:$ 10': 0.81; 'android.': 0.84; 'const': 0.84; 'end.': 0.84; 'env': 0.84; 'together,': 0.84; 'subject:Android': 0.93
Date Tue, 12 Nov 2013 12:44:12 +0100
From Dominik Weinhold <djweinhold@web.de>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.0
MIME-Version 1.0
To python-list@python.org
Subject Python and Android: PyImport_ImportModule always returns NULL
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V03:K0:oxDsoMbQ9HNULyYLI1M13KaYNj+polA9hCihUS7DCSbuyiKohW5 JcAEW4lNK9vJLvRjgUUMXzvZRyqkLCTEU3+DrRowTtyHg9plxG2t+xlTL+s8mSzFFLAyBwk aWfnMQpXFenKWqa0rcbYbUN5UA3Wr7QDhc/oVdvGlzjXOlqXolVLgL/Eeplu4CHYAiZL4Dz qMnrxbxbeWlmw3uzVlCgQ==
X-Mailman-Approved-At Tue, 12 Nov 2013 12:50:16 +0100
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2458.1384257017.18130.python-list@python.org> (permalink)
Lines 92
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384257017 news.xs4all.nl 15905 [2001:888:2000:d::a6]:35721
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59188

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Python and Android: PyImport_ImportModule always returns NULL Dominik Weinhold <djweinhold@web.de> - 2013-11-12 12:44 +0100

csiph-web