Tuesday, January 8, 2013

distribute/setuptools for PY4A

[UPDATE 2013-05-29] I have to say, don't bother with distribute or setuptools. This is what I do.

distribute/setuptools on python for android

**Beware this may break SL4A Python interpreter, but you will still be able to use standalone shell script Python. Also you will need root unless you have Python headers.**

[UPDATED 2013-04-06]
  1. add .pydistutils.cfg in $HOME (usually /data). If you have root access, you can make sdcard executable or write to /data/local/ (similar to /usr/local/ on Linux) and/or /data/data/com.googlecode.pythonforandroid/ (the PY4A directory). I don't necessarily recommend the install locations below, but you will need to put something so that setuptools knows where to install, since PY4A doesn't use the traditional Python file-structure. Also if bin isn't on your path, then you will need to use full pathnames for your shell commands. I used EXTERNAL_STORAGE=/mnt/sdcard/ on my VZW HTC Droid Incredible (adr6300) but on my VZW Motorola Droid RAZR HD (XT926) I use EXTERNAL_STORAGE=/storage/sdcard0/, so the location of your external storage may change.

  2. [install]
    install-purelib=/mnt/sdcard/com.googlecode.pythonforandroid/extras/python/site-packages
    install-platlib=/data/local/lib/python2.6/site-packages
    install-scripts=/data/local/bin


  3. add sitecustomize.py to any folder in $PYTHONPATH to process .pth files. I put this in /mnt/sdcard/com.googlecode.pythonforandroid/extras/python/ and it seemed to work fine.

  4. import site
    SITEPKGS = "/mnt/sdcard/com.googlecode.pythonforandroid/extras/python/site-packages"
    site.addsitedir(SITEPKGS)


  5. add site-packages to python shell script (highlighted yellow). You need both step 2 and step 3. [UPDATE 2013-04-06] I don't think that you have to do both - either use a sitecustomize.py file or add the new site-packages folders to the path. Do both for each install folder in step 1.

    #! /system/bin/sh

    export EXTERNAL_STORAGE=/mnt/sdcard/
    PYTHONPATH=${PYTHONPATH}:${EXTERNAL_STORAGE}com.googlecode.pythonforandroid/extras/python

    PYTHONPATH=${PYTHONPATH}:${EXTERNAL_STORAGE}com.googlecode.pythonforandroid/extras/python/site-packages
    PYTHONPATH=${PYTHONPATH}:/data/data/com.googlecode.pythonforandroid/files/python/lib/python2.6/lib-dynload
    export PYTHONPATH
    export TEMP=${EXTERNAL_STORAGE}com.googlecode.pythonforandroid/extras/python/tmp
    export PYTHON_EGG_CACHE=$TEMP
    export PYTHONHOME=/data/data/com.googlecode.pythonforandroid/files/python
    export LD_LIBRARY_PATH=/data/data/com.googlecode.pythonforandroid/files/python/lib
    /data/data/com.googlecode.pythonforandroid/files/python/bin/python "$@"


  6. create lib, include, bin structure in Python directory if not already existing
    • these steps only necessary if you don't already have Python headers.
    • I use C4Droid on my device but you're better off using ndk.
    • download python 2.6.2 tarball, extract and run ./configure. Do not patch.
    • add lib/python2.6/config/ and put Makefile from python installation in it
    • add pyconfig.h and all Python headers to include/python2.6/
  7. download, extract and install distribute
    • run (as su) from one level up so that site.py doesn’t interfere since path always adds current folder to path first!

    # python ./distribute-0.6.28/ setup.py install

  8. Now you can run easy_install requests and it will install Requests. But, libc from bionic is crazy, and most code does not run without patches, so I recommend using only pure python packages. In that case you could just copy the pure python modules to your site-packages folder and create a sitecustomize.py.
Fork me on GitHub