joaoventura / pybridge Goto Github PK
View Code? Open in Web Editor NEWReuse Python code in native Android applications
Reuse Python code in native Android applications
You must also copy the standard library files in python-for-android/dists/.../_python_bundle to assets/python along with the bootstrap.py file. If you don't do this, the Python interpreter will not start and the application will stop!.
Is it like this ? I tried it and really force close when opening the application.
is it because I'm using a different Arch? I'm using Arch x86
Hi,
has anyone tested it recently ?
It used to send stderr to logcat correctly, permitting us to debug an app easily, but it's not the case anymore.
If there is a problem in my Python code (improper code, exception raised), now the call terminates with this error :
I/pybridge: Initializing the Python interpreter
I/pybridge: Call into Python interpreter
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4 in tid 9120 (.jventura.pyapp), pid 9120 (.jventura.pyapp)
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
With no log of what went wrong.
I use Android Studio 3.1.4 (linux) and crystax-ndk-10.3.2 (but this version of crystax used to work).
Any idea ?
Regards.
有没有编译好的so库,我是新手,
万分感谢!
邮箱:[email protected]
我问一下能不能把python编译器加上一些包比如requests等等封装到位的app上 然后可以执行.py文件或者单行代码 你这么怎么用呢
Not sure if this is the right place to post the issue, as it may be more related to the crystax ndk, but I thought I'd give it a shot and see if there is a solution or workaround!
I'm attempting to use pybridge to make HTTP post requests from python code. At first I installed the requests
library into the assets/python folder, but then it was raising an ImportError as it was missing the C extension called _socket
. So I copied over _socket.so
from crystax-ndk/sources/python/3.5/libs/arm64-v8a/modules
but then I get a seg fault when calling requests.post
:
11-11 09:59:21.919 28840-28840/com.jventura.pyapp A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x8 in tid 28840 (.jventura.pyapp)
I've tracked down the error to a call made to _socket.getaddrinfo
. I've tried recompiling _socket.so
using socketmodule.c from the Python source code and the crystax ndk, but I still get the same error. I've also tried both armeabi-v7a
and arm64-v8a
architectures.
Any thoughts into how I could solve this problem? Is there a workaround that allows me to make http requests within the python code?
Thanks for your time
Hi there,
As you known, the stdlib.zip contains the base libs for python, if you want to use another lib, NumPy e.g, you could get it from : https://github.com/numpy/numpy, then how to add it to python?
Thanks.
In https://github.com/joaoventura/pybridge#running-the-project
You should change "Finally, open the terminal and run path/to/crystax/ndk-build" to "Finally, open the terminal, cd to app/src/main/jni, and run path/to/crystax/ndk-build"
Otherwise you get the error:
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
/home/jrm/Android/Crystax/crystax-ndk-10.3.2/build/core/build-local.mk:143: *** Android NDK: Aborting . Stop.
Thanks!
I downloaded your pybridge but it dosent work...
the error is java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader ~~ " couldn't find libpybridge.so "
how to fix it?
I ran the ndk-build to create libcrystax.so
, libpybridge.so
and libpython3.5m.so
in my src/main/libs/armeiabi-v7a
however when trying to run the app on a x86 emulator it cannot find libpybridge.so
. I copied over the armeabi-v7a
libpybridge.so
to an x86
folder and then get an unexpected e_machine: 40
error presumably because it's the wrong compilation. How do I get a x86 copy of libpybridge.so
and other versions?
Thanks for the Project.
Would like to know if there is a way to hide my python logic (bootstrap.py code) so that it does not get exposed.
Have few python files which I am trying to run along with my Android native files (C/CPP) - Can you please throw some lights on this?
My first foray into the Android NDK so perhaps I'm doing this wrong, but having just changed the name of the package from com.jventura
to com.randompkg
and then changing pybridge.c
to
JNIEXPORT jint JNICALL Java_com_randompkg_pybridge_PyBridge_start
JNIEXPORT jint JNICALL Java_com_randompkg_pybridge_PyBridge_stop
JNIEXPORT jint JNICALL Java_com_randompkg_pybridge_PyBridge_call
causes me to get this stack trace:
11-13 22:03:28.172 18981-18981/com.jventura.pyapp E/art: No implementation found for int com.randompkg.pybridge.PyBridge.start(java.lang.String) (tried Java_com_randompkg_pybridge_PyBridge_start and Java_com_randompkg_pybridge_PyBridge_start__Ljava_lang_String_2)
11-13 22:03:28.173 18981-18981/com.jventura.pyapp D/AndroidRuntime: Shutting down VM
11-13 22:03:28.174 18981-18981/com.jventura.pyapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jventura.pyapp, PID: 18981
java.lang.UnsatisfiedLinkError: No implementation found for int com.randompkg.pybridge.PyBridge.start(java.lang.String) (tried Java_com_randompkg_pybridge_PyBridge_start and Java_com_randompkg_pybridge_PyBridge_start__Ljava_lang_String_2)
at com.randompkg.pybridge.PyBridge.start(Native Method)
at com.randompkg.pyapp.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
PyBridge.java remains as:
package com.randompkg.pybridge;
import org.json.JSONObject;
import org.json.JSONException;
public class PyBridge {
...
Is there anything else I need to change?
can you share libpybridge.so file? i use aide and cant build ndk
Hi there,
Is it possible to retain a reference to a python thread, while it is working, so that you can call stop()
on it before it finishes? Or similarly, can you send the 'Ctrl+c' keyboard interrupt to a python thread through the pybridge interface?
The reason I ask is because I am getting SIGSEGV errors when making another network request through pybridge while a network request is ongoing (the network request is being done in python using the requests
module).
Hi,
Im using pybridge to evaluate the use of Python in Android Java apps. I built the Python packages for x86 using the p4a command installed from pip. I then copied the binaries created in build and dists to the folder python-for-android in the root of this project. I was able to build the pybridge library using ndk-build.
The build using android-studio and running in the emulator had no errors. But the app doesnt seem to start. It starts up and crashes without any messages in logcat. I can see error messages as in below in studio editor. Please help me in bringing up the app.
Forgive my poor understanding, I do not know how to do this step----run path / to / crystalline / ndk-build
Starting from your template, I made the following modifications:
Added to Android.mk:
# Include libsqlite3.so
include $(CLEAR_VARS)
LOCAL_MODULE := libsqlite3
LOCAL_SRC_FILES := $(CRYSTAX_PATH)/sources/sqlite/3/libs/$(TARGET_ARCH_ABI)/libsqlite3.so
LOCAL_EXPORT_CFLAGS := -I $(CRYSTAX_PATH)/sources/sqlite/3/include/
include $(PREBUILT_SHARED_LIBRARY)
Running ndk-build resulted in libsqlite3.so showing up in my project.
In bootstrap.py I added some quick-and-dirty test code:
def greet(args):
"""Simple function that greets someone."""
print(0)
import sqlite3
print(1)
try:
conn = sqlite3.connect("test.db")
print(sqlite3.version)
except Error as e:
print(e)
finally:
conn.close()
return 'Hello %s' % args['name']
This prints 0 to the log, but then gives
12412-12412/com.jventura.pyapp A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4 in tid 12412 (.jventura.pyapp)
What am I missing?
Thanks for this great library, BTW!
In the Application.mk
, APP_ABI
is set to armeabi-v7a x86
.
Is there a reason for this? Maybe a limitation of the python interpreter?
I would like to set it to APP_ABI := all
.
java.lang.UnsatisfiedLinkError
Hi, joaoventura
When I want to embed Python in Android,I discover your blog--http://joaoventura.net/blog/2014/python-android/ .Thank for you share this bolg.It help me a lot.But,you use Eclipse to build it,I use android studio.I can build libpython.so.But when I want to run project,it can't find Python.h.I don't know how to fix it.Can you help me?
I think the stdlib.zip is lacking some modules. Like select, _ctypes or _socket.
This is error log:
File "/data/user/0/com.jventura.pyapp/assets/python/stdlib.zip/platform.py", line 115, in
File "/data/user/0/com.jventura.pyapp/assets/python/stdlib.zip/subprocess.py", line 438, in
ImportError: No module named 'select'
I want to run a Django web server on my native android device using java code. So for that, I need to install some packages using pip install, since no documentation is provided with the code example here I am curious how to install it also will pyBridge support running Django webserver?
Hi, I have followed all steps,
I have also created the libpybridge.so inside src/main/libs using ndk-build
But still I am facing java.lang.UnsatisfiedLinkError: dlopen failed: library "libpybridge.so" not found
At System.loadLibrary("pybridge");
Any help would be appreciated
Hey, any chance you could whip up an example using Python 2.7?
I tried just changing the Android.mk file to point to CrystaX's 2.7 sources, however I get errors in the pybridge.c file when executing ndk-build
jni/pybridge.c:39:15: error: variable 'AndroidlogModule' has initializer but incomplete type
static struct PyModuleDef AndroidlogModule = {
^
I'd love to use an existing Python library in a new Android app which is why I'm looking into this, but the library is written in Python 2.7 which I have no experience with so this is all new to me! I've appreciated your blog posts and this project for showing me that it's possible so thanks so far.
Hi, I encounter some problem.
Any body help me?
Please add a license (MIT, for example). At the moment everyone has to assume that all the code is all rights reserved and can't be used.
Hii @joaoventura can u please publish the app apk file in this resporitory . Coz it require lot of file that I don't have .if any other person have that app made then please send a link to download it in the comment.THANKS FOR UR HELP
I have a problem
from PIL import Image
2019-09-06 09:52:02.937 10551-10551/com.jventura.pyapp I/pybridge: File "/data/user/0/com.jventura.pyapp/assets/python/stdlib.zip/PIL/Image.py", line 95, in
2019-09-06 09:52:02.937 10551-10551/com.jventura.pyapp I/pybridge: ImportError: cannot import name '_imaging'
_imaging.pyd file is in the PIL folder.
I put PIL in stdlib.zip
Can you help me?
Hey, how can i include numpy library in this project ?
Hello, How to install crystax-ndk in Mac? I not way to find it.
https://brewinstall.org/install-crystax-ndk-on-mac-with-brew/
Homebrew Install success.
but execute brew install crystax-ndk
error:
zhangkuixun$ brew install crystax-ndk
Error: No available formula with the name "crystax-ndk"
It was migrated from homebrew/core to homebrew/cask.
You can access it again by running:
brew tap homebrew/cask
And then you can install it by running:
brew cask install crystax-ndk
Hi ,
can please explain me briefly how to install crystax NDK in linux ?
java.lang.UnsatisfiedLinkError: dlopen failed: library "libpybridge.so" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1087)
at java.lang.Runtime.loadLibrary0(Runtime.java:1008)
at java.lang.System.loadLibrary(System.java:1664)
at com.jventura.pybridge.PyBridge.(PyBridge.java:50)
at com.jventura.pybridge.PyBridge.start(Native Method)
at com.jventura.pyapp.MainActivity.onCreate(MainActivity.java:34)
Hello, i'm trying to add the module PIL that based on c libraries:
Here i found the download link with the pil module and c classes for python 3
https://archlinuxarm.org/packages/aarch64/python-pillow
I'm adding them to the project and all time i'm getting error like
No module named '_imaging' - Imaging.h is c file
But the c files is already in assets, how to properly configure it. Thanks for any help.
Hi Sir.
There is a build error when cmake runs during build. It shows error at the following line of code in android.toolchain.cmake
if(NOT EXISTS "${ANDROID_NDK}/source.properties")
message(FATAL_ERROR "Missing file: ${ANDROID_NDK}/source.properties. Please use NDK r12+.")
endif()
I have error, not found select
in stdlib.zip
Initializing the Python interpreter
--------------------
--PyRun_SimpleString("import bootstrap");
Traceback (most recent call last):
File "", line 1, in
File "/data/user/0/com.jventura.pyapp/assets/python/bootstrap.py", line 13, in
import platform
File "/data/user/0/com.jventura.pyapp/assets/python/stdlib.zip/platform.py", line 115, in
File "/data/user/0/com.jventura.pyapp/assets/python/stdlib.zip/subprocess.py", line 438, in
ImportError: No module named 'select'
--------------------
File directory:
assets
--python
----bootstrap.py
----stdlib.zip
Traceback (most recent call last):
File "", line 1, in
File "/data/user/0/com.jventura.pyapp/assets/python/bootstrap.py", line 9, in
import ssl
File "/data/user/0/com.jventura.pyapp/assets/python/stdlib.zip/ssl.py", line 98, in
ImportError: No module named '_ssl'
Hey there,
My python script uses the following modules:
import imageio
from PIL import Image
import cv2
mport numpy as np
from numpy import array
I've been trying to add some external pip modules to the stdlib.zip
When I install packages through pip it goes to:
/site-packages/MODULE_NAME
So I figured it might goes the same in the stdlib.zip to add folder /site-packages
Needless to say it did not work.
Traceback (most recent call last):
File "", line 1, in
File "/data/user/0/com.jventura.pyapp/assets/python/bootstrap.py", line 14, in
from numpy import array
pybridge: ImportError: No module named 'numpy'
Bottom line, How do I add modules properly to the stdlib.zip?
Hi,
is there a way to include the $(CRYSTAX_PATH)/sources/python/3.5/libs/$(TARGET_ARCH_ABI)/modules/select.so
file for all architectures?
I know I can copy the select.so
to the assets/python
folder - that works. But that will only include it for one architecture.
I tried it like this, in the Android.mk
, but the python interpreter can't find it:
# Include select.so
include $(CLEAR_VARS)
LOCAL_MODULE := select
LOCAL_SRC_FILES := $(CRYSTAX_PATH)/sources/python/3.5/libs/$(TARGET_ARCH_ABI)/modules/select.so
include $(PREBUILT_SHARED_LIBRARY)
Error:
com.jventura.pyapp I/pybridge: ImportError: No module named 'select'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.