Python running on command line via ADB, PATH help needed.

Edge-Case

Senior Member
Jul 10, 2013
97
25
0
I have python working on ADB from the command line, I wasn't happy about having to develop with that little virtual keyboard so I made some changes and wrote a short shell script to launch python after setting up some variables.

What I did exactly:

1) I created a blank.img file on my pc and formated it to ext2 as if I was making a chroot image for ubuntu but instead I left it blank. I then named it apps.img but that doesn't matter.

2) I pushed it to my phone ( 4 gigs just for fun)

3) I mounted it via Terminal Emulator on my phone, had to use bash to get the syntax I used on the pc to work with "mount -o loop " . created a folder in /mnt called "myside" so "/mnt/myside" and mounted into "/mnt/myside"

4) I copied all the files, libs, execs, etc into/onto the img file

5) I put my script in "/system/bin" , named it "python" and made it executable, so "/system/bin/python"

6) I tested via "adb shell"


Code:
[email protected]:~$ adb shell
[email protected]:/ $ su
[email protected]:/ # python
Python 2.7.2 (default, Jun  3 2013, 20:01:13) 
[GCC 4.4.3] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import os
>>> import math
>>> import io
ANDROID_PRIVATE = /mnt/myside/com.hipipal.qpyplus0/files
>>> import android
>>> import twisted
>>> import PIL
>>> print(os.environ)
{'EXTERNAL_STORAGE': '/storage/emulated/legacy', 'LOOP_MOUNTPOINT': '/mnt/obb', 'BOOTCLASSPATH': '/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar', 'ANDROID_PROPERTY_WORKSPACE': '8,49152', 'USER': 'shell', 'PATH': '/mnt/myside/com.hipipal.qpyplus0/files/bin:/mnt/myside/com.hipipal.qpyplus2/scripts/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin', 'HOME': '/', 'ANDROID_STORAGE': '/storage', 'LD_LIBRARY_PATH': '/mnt/myside/com.hipipal.qpyplus2/lib:/vendor/lib:/system/lib', 'EMULATED_STORAGE_TARGET': '/storage/emulated', 'SHELL': '/system/bin/sh', 'ANDROID_ASSETS': '/system/app', 'ANDROID_BOOTLOGO': '1', '_': '/mnt/myside/com.hipipal.qpyplus0/files/bin/python', 'TERMINFO': '/system/etc/terminfo', 'MKSH': '/system/bin/sh', 'PYTHONOPTIMIZE': '2', 'ANDROID_PRIVATE': '/mnt/myside/com.hipipal.qpyplus0/files', 'ANDROID_SOCKET_adbd': '9', 'SECONDARY_STORAGE': '/storage/sdcard1', 'PYTHONPATH': '/mnt/myside/com.hipipal.qpyplus2/lib/site-packages:/mnt/myside/com.hipipal.qpyplus0/files/lib/site-packages:/mnt/myside/com.hipipal.qpyplus0/files/lib/lib-dynload:/mnt/myside/com.hipipal.qpyplus0/files/lib/config:/mnt/myside/com.hipipal.qpyplus0/files/lib/python27:/mnt/myside/com.hipipal.qpyplus0/files/lib/QPython', 'RANDOM': '29772', 'ANDROID_DATA': '/data', 'TERM': 'linux', 'EMULATED_STORAGE_SOURCE': '/mnt/shell/emulated', 'TMPDIR': '/mnt/myside/com.hipipal.qpyplus2/cache', 'HOSTNAME': 'android', 'ANDROID_ROOT': '/system', 'PYTHONHOME': '/mnt/myside/com.hipipal.qpyplus0/files:/mnt/myside/com.hipipal.qpyplus0/files/bin/python', 'ASEC_MOUNTPOINT': '/mnt/asec', 'ANDROID_CACHE': '/cache'}
>>> dir(PIL)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>> dir(twisted)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_version', 'python', 'version']
>>> from PIL import image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name image
>>> dir(android)
['AndroidBrowser', 'AndroidService', 'BuildInfo', 'KEYCODE_0', 'KEYCODE_1', 'KEYCODE_2', 'KEYCODE_3', 'KEYCODE_4', 'KEYCODE_5', 'KEYCODE_6', 'KEYCODE_7', 'KEYCODE_8', 'KEYCODE_9', 'KEYCODE_A', 'KEYCODE_ALT_LEFT', 'KEYCODE_ALT_RIGHT', 'KEYCODE_APOSTROPHE', 'KEYCODE_AT', 'KEYCODE_B', 'KEYCODE_BACK', 'KEYCODE_BACKSLASH', 'KEYCODE_C', 'KEYCODE_CALL', 'KEYCODE_CAMERA', 'KEYCODE_CLEAR', 'KEYCODE_COMMA', 'KEYCODE_D', 'KEYCODE_DEL', 'KEYCODE_DPAD_CENTER', 'KEYCODE_DPAD_DOWN', 'KEYCODE_DPAD_LEFT', 'KEYCODE_DPAD_RIGHT', 'KEYCODE_DPAD_UP', 'KEYCODE_E', 'KEYCODE_ENDCALL', 'KEYCODE_ENTER', 'KEYCODE_ENVELOPE', 'KEYCODE_EQUALS', 'KEYCODE_EXPLORER', 'KEYCODE_F', 'KEYCODE_FOCUS', 'KEYCODE_G', 'KEYCODE_GRAVE', 'KEYCODE_H', 'KEYCODE_HEADSETHOOK', 'KEYCODE_HOME', 'KEYCODE_I', 'KEYCODE_J', 'KEYCODE_K', 'KEYCODE_L', 'KEYCODE_LEFT_BRACKET', 'KEYCODE_M', 'KEYCODE_MEDIA_FAST_FORWARD', 'KEYCODE_MEDIA_NEXT', 'KEYCODE_MEDIA_PLAY_PAUSE', 'KEYCODE_MEDIA_PREVIOUS', 'KEYCODE_MEDIA_REWIND', 'KEYCODE_MEDIA_STOP', 'KEYCODE_MENU', 'KEYCODE_MINUS', 'KEYCODE_MUTE', 'KEYCODE_N', 'KEYCODE_NOTIFICATION', 'KEYCODE_NUM', 'KEYCODE_O', 'KEYCODE_P', 'KEYCODE_PERIOD', 'KEYCODE_PLUS', 'KEYCODE_POUND', 'KEYCODE_POWER', 'KEYCODE_Q', 'KEYCODE_R', 'KEYCODE_RIGHT_BRACKET', 'KEYCODE_S', 'KEYCODE_SEARCH', 'KEYCODE_SEMICOLON', 'KEYCODE_SHIFT_LEFT', 'KEYCODE_SHIFT_RIGHT', 'KEYCODE_SLASH', 'KEYCODE_SOFT_LEFT', 'KEYCODE_SOFT_RIGHT', 'KEYCODE_SPACE', 'KEYCODE_STAR', 'KEYCODE_SYM', 'KEYCODE_T', 'KEYCODE_TAB', 'KEYCODE_U', 'KEYCODE_UNKNOWN', 'KEYCODE_V', 'KEYCODE_VOLUME_DOWN', 'KEYCODE_VOLUME_UP', 'KEYCODE_W', 'KEYCODE_X', 'KEYCODE_Y', 'KEYCODE_Z', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__', 'accelerometer_enable', 'accelerometer_enabled', 'accelerometer_reading', 'ack_stop', 'action_send', 'check_pause', 'check_stop', 'get_buildinfo', 'get_dpi', 'hide_keyboard', 'init', 'map_key', 'open_url', 'show_keyboard', 'start_service', 'stop_service', 'vibrate', 'wait_for_resume', 'webbrowser', 'wifi_scan', 'wifi_scanner_enable']
>>> help(twisted)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'help' is not defined
>>> help(os)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'help' is not defined
>>> 
[email protected]:/ # set
ANDROID_ASSETS=/system/app
ANDROID_BOOTLOGO=1
ANDROID_CACHE=/cache
ANDROID_DATA=/data
ANDROID_PROPERTY_WORKSPACE=8,49152
ANDROID_ROOT=/system
ANDROID_SOCKET_adbd=9
ANDROID_STORAGE=/storage
ASEC_MOUNTPOINT=/mnt/asec
BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar
COLUMNS=80
EMULATED_STORAGE_SOURCE=/mnt/shell/emulated
EMULATED_STORAGE_TARGET=/storage/emulated
EXTERNAL_STORAGE=/storage/emulated/legacy
HOME=/
HOSTNAME=android
IFS=' 	
'
KSHEGID=0
KSHGID=0
KSHUID=0
KSH_VERSION='@(#)MIRBSD KSH R40 2011/10/07'
LD_LIBRARY_PATH=/vendor/lib:/system/lib
LINES=24
LOOP_MOUNTPOINT=/mnt/obb
MKSH=/system/bin/sh
OPTIND=1
PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin
PGRP=4849
PIPESTATUS[0]=0
PPID=4844
PS1='$(precmd)[email protected]$HOSTNAME:${PWD:-?} # '
PS2='> '
PS3='#? '
PS4='+ '
PWD=/
RANDOM=5114
SECONDARY_STORAGE=/storage/sdcard1
SECONDS=289
SHELL=/system/bin/sh
TERM=linux
TERMINFO=/system/etc/terminfo
TMOUT=0
USER=shell
USER_ID=0
_=set
[email protected]:/ #

And this is the script that I used to launch it...

Code:
#!/system/bin/sh

export ANDROID_PRIVATE="/mnt/myside/com.hipipal.qpyplus0/files"

export LD_LIBRARY_PATH="/mnt/myside/com.hipipal.qpyplus2/lib:$LD_LIBRARY_PATH"

export PATH="/mnt/myside/com.hipipal.qpyplus0/files/bin:/mnt/myside/com.hipipal.qpyplus2/scripts$PATH"

export PYTHONHOME="/mnt/myside/com.hipipal.qpyplus0/files:/mnt/myside/com.hipipal.qpyplus0/files/bin/python"

export PYTHONOPTIMIZE="2"

export PYTHONPATH="/mnt/myside/com.hipipal.qpyplus2/lib/site-packages:/mnt/myside/com.hipipal.qpyplus0/files/lib/site-packages:/mnt/myside/com.hipipal.qpyplus0/files/lib/lib-dynload:/mnt/myside/com.hipipal.qpyplus0/files/lib/config:/mnt/myside/com.hipipal.qpyplus0/files/lib/python27:/mnt/myside/com.hipipal.qpyplus0/files/lib/QPython"

export TMPDIR="/mnt/myside/com.hipipal.qpyplus2/cache"

/mnt/myside/com.hipipal.qpyplus0/files/bin/python

I was just gonna add the variables to my "mkshrc" file but I found problems with that chroot'ing into linux, I guess its best for now to set it up in a script that way it doesn't effect the global system so to speak, I just hope that if another program or script calles python it will work like normal ( i.e. set up the variables and launch python, or run said script in /system/bin called python)

What do you think I am missing?

I am almost certain that the normal program that does the launching does some strange linking of libraries and thats why I am missing stuff like the twisted set of modules etc, it got twisted's main file but not its sub files, I am guessing the folder in the same dir that twisted file is in will contain what I need. Do I need to add all those folders to my PYTHONPATH?
 

Edge-Case

Senior Member
Jul 10, 2013
97
25
0
Ha! I tried running python from Qpython's app w/ a fresh install and 'help()' didn't work there either.

2) Its 'from PIL import Image'


I'm starting to think i had it right the first time, irony.
 
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone