From 562b852514534b05882a67ed6dafb98868c650de Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Fri, 18 Jan 2013 16:34:22 -0500 Subject: [PATCH 1/2] Add a |python| wrapper script which sets the right environment variables and invokes /data/python/bin/python. Crucially, the wrapper script sets LD_PRELOAD=libpython2.7.so. Without this, we can't load any of the libs in lib-dynload, which means that we can't import any Python module which uses native code (e.g. the select module). Without LD_PRELOAD, we get an error like the following when we try to import a module which uses native code: >>> import subprocess Traceback (most recent call last): File "", line 1, in File "/data/python/lib/python2.7/subprocess.py", line 429, in import select ImportError: Cannot load library: reloc_library[1285]: 9814 cannot locate 'PyDict_Next'... The root problem is that the lib-dynload libraries do not have an explicit dependency on libpython.so (see readelf -d), so therefore the bionic linker doesn't try to resolve their symbols against libpython. Setting this dependency would be the Right Fix. --- README.md | 12 ++++-------- python | 6 ++++++ 2 files changed, 10 insertions(+), 8 deletions(-) create mode 100755 python diff --git a/README.md b/README.md index 340fb39..bf156f0 100644 --- a/README.md +++ b/README.md @@ -33,19 +33,15 @@ If your build was succesful then you can install Python using the `adb` tool, wh ``` $ adb push install/data/python /data/python +$ adb push python /system/bin/python +$ adb shell chmod 766 /system/bin/python ``` -Using -===== - -Once you have copied Python to your device you have to set `LD_LIBRARY_PATH`, `PYTHONHOME` and `PATH` correctly: +Now you should be able to run python with ``` $ adb shell -root@android:/data # export LD_LIBRARY_PATH=/data/python/lib -root@android:/data # export PYTHONHOME=/data/python -root@android:/data # export PATH=$PATH:/data/python/bin -root@android:/data # python +# python Python 2.7.3 (default, Dec 20 2012, 14:25:30) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. diff --git a/python b/python new file mode 100755 index 0000000..0ddb3e7 --- /dev/null +++ b/python @@ -0,0 +1,6 @@ +#!/system/bin/sh + +export LD_LIBRARY_PATH=/data/python/lib +export LD_PRELOAD=/data/python/lib/libpython2.7.so +export PYTHONHOME=/data/python +/data/python/bin/python $@ From 984536cb77bd6eb904a9061e691ca478a2f94bbd Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Fri, 18 Jan 2013 16:55:22 -0500 Subject: [PATCH 2/2] Don't nuke the LD_LIBRARY_PATH and LD_PRELOAD variables. You might want to pass an LD_LIBRARY_PATH to a child process of python, for example. --- python | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python b/python index 0ddb3e7..949f7bd 100755 --- a/python +++ b/python @@ -1,6 +1,6 @@ #!/system/bin/sh -export LD_LIBRARY_PATH=/data/python/lib -export LD_PRELOAD=/data/python/lib/libpython2.7.so +export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/python/lib +export LD_PRELOAD=$LD_PRELOAD:/data/python/lib/libpython2.7.so export PYTHONHOME=/data/python /data/python/bin/python $@