[Android Software/Hacking General [Developers Only]] 7175:
(Check out https://www.python.org for information on what python is.)

Yes you read that correctly, static python for Android! It took a while to figure out how to get this compiled, but I finally did it. I present you with an installer and a little bit of testing. The second post contains information on compilation and the sources. Btw, any cool python scripts are welcomed!

INSTALLATION

Recovery flash installers are attached that will install python 2.7.9 and/or 3.4.2 to /system/pythonX.X.X. You can install both if you want. Installation size is about 41MB, 43MB, and 49MB respectively for python 2.7.8, python 2.7.9, and python 3.4.2.

After installation, the python director(ies) in /system will contain the static python binary, some scripts, and a bunch of modules and documents. Separate scripted executables will be installed to /system/bin/python or /system/bin/python3 depending on which one is installed. These basically just set the PYTHONHOME environment variable and execute the python binary.

TESTING

When you boot up to Android after installation, you should be able to just open up a terminal and fire off some python commands.

Test using python 2.7.9:

python -c 'print "Hello World!"'

Test using python 3.4.2:

python3 -c 'print("Hello World!")'

You can also write scripts shelled with python:

#!/system/bin/python

print "Hello World!"

Make sure to set them as executable with "chmod +x".

Adjust your screen brightness via /sys (root required, tested on Galaxy Nexus):

python -c 'f=open("/sys/devices/omapdss/display0/backlight/s6e8aa0/brightness","w"); f.write("40"); f.close()'

A cool script I made to tweak file system I/O like rq_affinity, rotational, etc for I/O blocks:

#!/system/bin/python

import os,re,sys

list=[]
# find all directories containing rq_affinity
for roots, dirs, files in os.walk('/sys'):
for file in files:
match=re.search(r'\S+/rq_affinity',os.path.join(roots,file))
if match:
list.append(match.group().replace('rq_affinity',''))

# write specific values to files in each directory found before
for dir in list:
for name in 'rq_affinity', 'rotational', 'read_ahead_kb', 'nr_requests', 'iostats', 'nomerges', 'add_random':
try:
f=open(dir+name,'w')
if name is 'rq_affinity': f.write('1')
elif name is 'read_ahead_kb': f.write('512')
elif name is 'nr_requests': f.write('512')
else: f.write('0')
f.close()
except IOError:
sys.stderr.write('Problem writing to ' + dir+name + '\n')

Or execute "python" or "python3" without any parameters to open up the interpreter and go from there. When you start for example "python" (python 2.7.8), you should see something like:

Python 2.7.8 (default, Dec 2 2014, 05:15:18)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

***I haven't fully tested this yet, so let me know how it goes. I know basic commands work, but there are still some complexities in the compilation that may need to be figured out.***

(update) -- Install Extra Packages/Modules via "easy_install" -- (root required)

1) In a shell, remount /system read-writable with:

mount -o remount,rw /system

2) Change directory to /system/pythonX.X.X, i.e.,

cd /system/pythonX.X.X

3) Execute easy_install followed by a package name, i.e.,

./easy_install [i]package_name[/i]

4) Let it download and install! :) Then test it out.

5) Remount /system read-only with:

mount -o remount,ro /system

Also, to remove a package, execute:

./easy_install -m [i]package_name[/i]
rm -r ../lib/pythonX.X/site-packages/[i]package_name[/i]*.egg

*** Flashing the resolv.conf patch may be required to make an internet connection to download modules.

Also, a few things need to be done to get easy_install working:

1) For python 2.7.8 or 2.7.9 you need to link python2.7 to python like this:

ln -s /system/python2.7.9/bin/python2.7 /system/python2.7.9/bin/python

I'll make sure this is automatically done in future installs.

2) SSL certificates need to be added to "/etc/pki/tls/certs/" to use SSL with easy_install.

mkdir -p /etc/pki/tls/certs
curl http://curl.haxx.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt

ISSUES

The python installations have the following modules compiled in with external dependencies such as libraries not included at this point:

--------------PYTHON 2.7.8-2.7.9----------------------
_hashlib _multiprocessing _ssl
_testcapi bz2 crypt
dbm

--------------PYTHON 3.4.2-----------------------------
_crypt _dbm _decimal
_hashlib _multiprocessing _ssl
_testcapi

I will look into this more.

As [MENTION=3373153]bubbleguuum[/MENTION] points out, name resolution with python 3.4.2's urllib wasn't working. I found a workaround by adding the line

options single-request-reopen

to /system/etc/resolv.conf. This still needs some testing but seemed to work for me. I've included a recovery flash zip to patch /system/etc/resolv.conf if the line is not there since this is a root operation.

UPDATES

* 2014-12-02 * Compiled in more modules for each installation. This makes the installation a bit bigger, but it's worth it. You get a more complete python! :) To get stuff like help functions and math and readline modules, you need the larger installations linked below under "DOWNLOADS".

* 2014-12-03 * Thanks [MENTION=4584140]cybojenix[/MENTION] for pointing out some incapatibility issues with the original edify installer I was using. I have now updated the installers to use SuperSU's great non-edify sh-script installer.

* 2014-12-05 * Some updates to the installers to include more modules, plus size reduction for python 3.4.2. Modules added to python 2.7.8 : _bsddb _ctypes, _ctypes_test, _hotshot, _json, _lsprof, _sqlite3, future_builtins, and ossaudiodev, easy_install, pip(broken for now). Modules added to python 3.4.2: _bz2, _ctypes, _ctypes_test, _gdbm, _json, _lsprof, _opcode, _sqlite3, _testbuffer, _testimportmultiple, ossaudiodev, xxlimited

* 2014-12-05 * Some big updates to the installer to include almost the rest of the modules that wouldn't compile. Also easy_install is working on my end with these new experimental builds. With a little effort, should be able to get pip working as well. I had to use some hacky flags to get it to compile, ignore unresolved-symbols and such, so it definitely needs some testing. Another thing I did was tweak the install script to write over previous installations from these installations. I recommend downloading the experimental builds if you read this. The worst thing that might happen is one of the new modules I've included won't work completely.

* 2014-12-14 * Added python 2.7.9. Trimmed the installation sizes down to the much more reasonable 41MB, 43MB, and 49MB respectively for python 2.7.8, python 2.7.9, and python 3.4.2.

* 2014-12-24 * Attached a recovery flash.zip to patch resolv.conf to try and fix a urllib name resolution error.

* 2014-12-26 * A couple updates: Tweaked python installations so that modules looks for "/system/bin/sh" instead of "/bin/sh" when needed. Also a few "#!" corrections for scripts in the python bin directory. This shouldn't break anything from what I've tested, only make Android python more capable, but I'll keep the old installations attached for now just in case since the modifications involved using "sed". The second update is nice. I've added a little section on installing extra modules with easy_install(included and working with all installations).