在android上運行python腳本,或者在android上使用python交互界面,對熟悉python的研究或開發人員來說,是一件很有吸引力的事情,因為python腳本真是非常高效,另外,有很多非常好的庫
android官方目前沒有支持在android設備運行python,但是網上有一些項目組做了這個事情: http://geeknizer.com/install-run-python-apps-scripts-on-android/ 這個鏈接就提供好幾個項目,我稍微嘗試了 Kivy 這種方式, 需要安裝
的東西太多,就放棄了. 推薦 http://dinostest.com/node/28 這種方式,使用 qpython 項目, 可以用非常少的付出得到 android 上可以運行的 python 環境.
1. Please download QPython from url: http://qpython.com/ 這里我采用不同的方式,直接從360手機助手搜索並安裝 qpython , 更加方便
2. adb install qpython package into device or emulator
3. change /system folder to be readable and writeable
4. modify /system/etc/mkshrc to add following at the end of the file
ANDROID_PRIVATE=/data/data/com.hipipal.qpyplus/files;export ANDROID_PRIVATE
ANDROID_PUBLIC=/storage/sdcard/com.hipipal.qpyplus;export ANDROID_PUBLIC
ANDROID_SOCKET_zygote=9;export ANDROID_SOCKET_zygote
AP_HANDSHAKE=10e4c98a-4216-47a3-bf67-57b99d7d516e;export AP_HANDSHAKE
AP_HOST=127.0.0.1;export AP_HOST
AP_PORT=45367;export AP_PORT
LD_LIBRARY_PATH=/vendor/lib:/system/lib:/data/data/com.hipipal.qpyplus/files/lib:/data/data/com.hipipal.qpyplus/files:/data/data/com.hipipal.qpyplus/lib
export LD_LIBRARY_PATH
PATH=/data/data/com.hipipal.qpyplus/files/bin:/sbin:/vendor/bin:/system/bin:/system/xbin
export PATH
PYTHONHOME=/data/data/com.hipipal.qpyplus/files;export PYTHONHOME
PYTHONOPTIMIZE=2;export PYTHONOPTIMIZE
PYTHONPATH=/data/data/com.hipipal.qpyplus/files/lib/python2.7/:/data/data/com.hipipal.qpyplus/files/lib/python2.7/lib-dynload/:/data/data/com.hipipal.qpyplus/files/lib/python2.7/site-packages/:/storage/sdcard/com.hipipal.qpyplus/lib/python2.7/site-packages/:
export PYTHONPATH
PYTHONSTARTUP=/storage/sdcard/com.hipipal.qpyplus/lib/python2.7/site-packages/qpythoninit.py
export PYTHONSTARTUP
TMPDIR=/storage/sdcard/com.hipipal.qpyplus/cache;export TMPDIR
5 relogin the shell
6. type python in the adb shell
root@generic:/ # python // 我嘗試的時候,直接運行 python (其實是運行 /data/data/com.hipipal.qpyplus/files/bin/python ) 會報錯: error: only position independent executables (PIE) are supported. 我測試的設備是 Nexus 6 , android 6.0 , 在
這些比較新的 android 系統上有保護,需要可執行程序使用 -PIE 進行編譯,否則無法運行. 但我發現同樣的目錄下 python-android5 是可以運行的, 所以就 mv python python.bak ; ln -s python-android5 python ; 這樣后面就可以直接運行 python 了.