Skip to content
 

Cross Compiling Python for Embedded Linux

This article details how to cross compile Python for the ARM and PowerPC platforms.  It should apply equally to other platforms as well, just plug-in the correct cross-compiler.  The article supports Python versions, 2.6.6 (Thanks to evadeflow), 2.7.2 and 3.1.1, 3.1.2, 3.1.3, 3.2.2.

Firstly, download the Python that you want to use from http://www.python.org/

Unpack the Python package using tar:

tar -xvzf Python-2.7.2.tgz

This will create a directory called Python-2.7.2.  Goto the directory:

cd Python-2.7.2

Then run these commands to build the host components:

./configure

make python Parser/pgen

mv python hostpython

mv Parser/pgen Parser/hostpgen

make distclean

Download the correct patch for your version of Python:

Then apply the patch:

patch -p1 < Python-2.7.2-xcompile.patch

Then run this (where ~/Python-2.7.2/_install/ is your desired installation path).  Note also that you must replace all instances of the cross compiler and the host build system in the lines below.  If you are on an x86_64 machine, then you should use x86_64-linux-gnu as the host.

CC=ppc_6xx-gcc CXX=ppc_6xx-g++ AR=ppc_6xx-ar RANLIB=ppc_6xx-ranlib ./configure --host=ppc-linux --build=x86_64-linux-gnu --prefix=/python

make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED="ppc_6xx-gcc -shared" CROSS_COMPILE=ppc_6xx- CROSS_COMPILE_TARGET=yes HOSTARCH=ppc-linux BUILDARCH=x86_64-linux-gnu

make install HOSTPYTHON=./hostpython BLDSHARED="ppc_6xx-gcc -shared" CROSS_COMPILE=ppc_6xx- CROSS_COMPILE_TARGET=yes prefix=~/Python-2.7.2/_install

This will install all your python binaries and libraries in ~/Python-2.7.2/_install.

Copy the entire _install directory to the device, setup the PATH environment variable to include the path the Python executable and run:

python lib/python-2.7/test/test___all__.py

…and hopefully all the tests will run correctly.

To speed up the importing of Python modules on the target, I recommend to zip up the lib directory to make a file called python27.zip.  This means that we do not have to copy all the Python files to the target, just the one zip file.  This technique might also save space, but that depends on your file system.  On the host machine:

cd _install/lib/python2.7

zip -r -y python27.zip .

Delete libpythonxxx.a site-packages, lib-dynload, config and anything else you do not need from the python27.zip file.

Copy the _install/bin/python to the /usr/bin directory on the target:

Copy the python27.zip file to the /usr/lib directory on the target:

Create a directory on the target called python2.7 in the /usr/lib directory and copy the following directories to that directory:

  • ./lib/python2.7/config
  • ./lib/python2.7/lib-dynload
  • ./lib/python2.7/site-packages

Your directory structure on the target must be as follows:

/usr/lib # ls

python2.7 python27.zip

/python/lib/python2.7 # ls

config lib-dynload site-packages

Set the PYTHONHOME environment variable to /usr/ and you are ready to run Python on the target.

The above patches are based on Chris Lambacher’s patches for Python 2.5 here:

http://whatschrisdoing.com/blog/2006/10/06/howto-cross-compile-python-25/

Other links and credits:

http://www.ailis.de/~k/archives/19-ARM-cross-compiling-howto.html

104 Comments

  1. TS says:

    When I tried Dave L’s 2.7.2 patch i got this:
    ]# patch -p1 < 2.7.2.patch
    patching file configure
    patching file Makefile.pre.in
    Hunk #1 FAILED at 182.
    Hunk #2 FAILED at 215.
    Hunk #3 FAILED at 407.
    Hunk #4 FAILED at 542.
    Hunk #5 FAILED at 925.
    Hunk #6 FAILED at 1049.
    6 out of 6 hunks FAILED — saving rejects to file Makefile.pre.in.rej
    patching file setup.py

  2. Paul Gibson says:

    Hi TS,

    Yes, I have the same problem. I have a new patch that I will try and provide tonight.

    Paul

  3. TS says:

    Thank you very much. Your how-to helped me alot to get Python onto an Embedded Linux system for the company I work for. So far I have been able to successfull get Python 2.7.1 running on the system.

    Again, thanks a lot for taking your time to do all of this.

  4. TS says:

    I forgot to mention that using the 2.7.1 patch disables the ctypes module. Here is the lines that I think cause this problem:
    +# _ctypes fails to cross-compile due to the libffi configure script.
    +if os.environ.has_key(‘PYTHONXCPREFIX’):
    + disabled_module_list.append(‘_ctypes’)
    +

    When I used the patch and cross compiled it, I have ctypes in my library files, but not as a .so module in lib-dynload. Is there any way to fix the patch to allow for ctypes? What I tried so far to get around this problem was downloading and installing Python version 2.6.6, patching it with the 2.6.6 patch and then cross compiling it. Then I took the _ctypes.so from the 2.6.6 lib-dynload and then put it into my 2.7.1 cross compiled version. So far it seems to be working.

  5. CLaymore says:

    Hello all!

    I have this task, which is to make a build of Python 2.7 for the PowerPC CPU.
    I was able to follow this tutorial but i notice that my lib/python2.7/lib-dynload is empty…

    Did you guys had the same problem? If you did, how did you solve it?

    I noticed in the make install output that the make, copies the files to the right place but after that, it cleans the lib-dynload directory :S

  6. Paul Gibson says:

    Hi Claymore,

    It is working for me and the lib-dynload directory is here:
    lib/python2.7/lib-dynload/

    Paul

  7. Paul Gibson says:

    Patch for 2.7.2 and 3.2.2 has been posted.

  8. JaneS says:

    Hi all, please can you help me??? I just cross compile Python 3.2.2 and I have some problems with ELF. Python is normally cross compiles, but not modules.
    Make’s output is something like this :

    *** WARNING: renaming “_struct” since importing it failed: build/lib.linux-x86_64-3.2/_struct.cpython-32m.so: wrong ELF class: ELFCLASS32

    and in /build/tmp.linux-x86_64-3.2/libffi/config.log have this output log:

    /home/jane/libs/Python-3.2.2/Modules/_ctypes/libffi/configure: line 3699: ./conftest: cannot execute binary file
    configure:3701: $? = 126
    configure:3708: error: in `/home/jane/libs/Python-3.2.2/build/temp.linux-x86_64-3.2/libffi’:
    configure:3712: error: cannot run C compiled programs.

    P.S. I understand what the problem is in ./conftest binary file, but how can I solve this.
    in Crosscompiling I use the patch 3.2.2-xcompile.diff but without changesin setup.py.
    P.P.s. I have this problem and in python 2.2.1.
    PLEASE HELP!!!

  9. jaux says:

    Cross-compiling Python 2.7 without ctypes is not cool, I’ll try to work it out next few days, stay turned ;)

  10. jaux says:

    @paul, please contact me, I have the patch for 2.7.2 with ctypes enabled.

  11. apl says:

    @jaux: your 2.7.2 ctypes patch did not work for me :(
    Compiling for arm-unknown-linux-gnueabi-. Does anybody have the solution?
    Thanks!

    --- snip ---
    /home/apl/python/Python-2.7.2/Modules/_ctypes/cfield.c: In function 'PyCField_FromDesc':
    /home/apl/python/Python-2.7.2/Modules/_ctypes/cfield.c:50:29: warning: variable 'length' set but not used [-Wunused-but-set-variable]
    /home/apl/python/Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c: In function 'mmap_resize':
    /home/apl/python/Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c:3193:5: warning: implicit declaration of function 'mremap' [-Wimplicit-function-declaration]
    /home/apl/python/Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c: In function 'sys_trim':
    /home/apl/python/Python-2.7.2/Modules/_ctypes/libffi/src/dlmalloc.c:3612:62: warning: comparison between pointer and integer [enabled by default]
    /tmp/ccgf4gVM.s: Assembler messages:
    /tmp/ccgf4gVM.s:1388: Error: bad instruction `icbi 0,r0'
    /tmp/ccgf4gVM.s:1388: Error: bad instruction `dcbf 0,ip'
    /tmp/ccgf4gVM.s:1397: Error: bad instruction `icbi 0,r4'
    /tmp/ccgf4gVM.s:1397: Error: bad instruction `dcbf 0,r5'
    /tmp/ccgf4gVM.s:1406: Error: bad instruction `icbi 0,r4'
    /tmp/ccgf4gVM.s:1406: Error: bad instruction `dcbf 0,r5'
    /tmp/ccgf4gVM.s:1415: Error: bad instruction `icbi 0,r4'
    /tmp/ccgf4gVM.s:1415: Error: bad instruction `dcbf 0,r5'
    /tmp/ccgf4gVM.s:1424: Error: bad instruction `icbi 0,r4'
    /tmp/ccgf4gVM.s:1424: Error: bad instruction `dcbf 0,r5'
    /tmp/ccgf4gVM.s:1433: Error: bad instruction `icbi 0,r0'
    /tmp/ccgf4gVM.s:1433: Error: bad instruction `dcbf 0,r4'
    /tmp/ccgf4gVM.s:1433: Error: bad instruction `sync'
    /tmp/ccgf4gVM.s:1433: Error: bad instruction `isync'
    --- snip ---

    --- snip ---
    Failed to build these modules:
    _ctypes
    --- snip ---
  12. Jack says:

    HI paul
    i’m from china and my english is pool~ i’m a newer and i tried to follow you instruction .unfortrunately i fail make as i got the error: arm-none-linux-gnueabi-gcc: directory”: No such file or directory
    : warning: missing terminating ” character
    ./Modules/getbuildinfo.c: In function ‘_Py_svnversion’:
    ./Modules/getbuildinfo.c:63: error: missing terminating ” character
    ./Modules/getbuildinfo.c:63: error: expected expression before ‘;’ token

    i have no idea how to fix this . could you help me please thanks !!!

  13. Jack says:

    @Paul Gibson – I just follow the commands you posted and when i finishing patching i made the setting :
    CC=arm-linux-gcc CXX=arm-linux-g++ AR=arm-linux-ar RANLIB=arm-linux-ranlib ./configure –host=arm-linux –build=i386-linux –prefix=$PREFIX

    i successfully passed the configure step but failed in the make step:

    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-linux-gcc -shared” CROSS_COMPILE=arm-linux- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=i386-linux

    the error imformation was shown above.

    do you have any ideas ? thanks!

  14. Michael says:

    Hi,
    I could crosscompile and easily install this on my ARM9 System.
    Python runs – so thanks for the great work. Anyway I’m missung some modules.

    The cross compiling said:

    Python build finished, but the necessary bits to build these modules were not
    found:
    _bsddb _curses _curses_panel
    _sqlite3 _ssl _tkinter
    bsddb185 bz2 dbm
    dl gdbm imageop
    nis readline sunaudiodev
    zlib
    To find the necessary bits, look in setup.py in detect_modules() for the
    module’s name.

    Failed to build these modules:
    _ctypes

    How can I get sqlite3? I need it for my app, is there anything I can do?

  15. William Ward says:

    Thanks for posting this cross-compilation tutorial – one device came with Python preloaded, but I have three others (two BugLabs Bugs) and a rooted Nook Color that I’d like to give a go at compiling newer versions of Python for, and subsequent modules.

    A clear tutorial helps a great deal!

    :)

  16. Paul Gibson says:

    I noticed today that the 2.6 and 2.7 patches above are not working on Linux 3.0 host machines such as Ubuntu 11.10. I have updated version 2.6.6 and 2.7.2 to include a patch that works on Linux 3.0 hosts. Python 3 patches are not affected by this.

    I have also removed all previous 2.6 and 2.7 patches to make the site cleaner.

  17. yesuraj says:

    Hi,
    After following the above steps, i successfully compiled python for arm architecture. But at the end i don’t find the libpython2.7.so.1.0 anywhere.
    Can some one help me in this regard

  18. Paul Gibson says:

    Hi yesuraj,

    We don’t currently build a shared library, only the static libpython2.7.a is generated. You should link against this instead. The shared library isn’t required for running python on the target.

    Paul

  19. yesuraj says:

    Hi,
    I compiled successfully, but i am not able to use ncurses modules in my python scripts. I hope ncurses modules are not build along. I installed ncurses in my ubuntu (The host machine OS that i use, it is “2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux” machine) host machine.
    When i do host configuration, that is before applying patches if i do ./configure
    the ncurses libs status shows as follows,

    checking ncurses.h usability… yes
    checking ncurses.h presence… yes
    checking for ncurses.h… yes

    But after applying patches if i do the follow configure command

    CC=/usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-gcc CXX=/usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-g++ AR=/usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-ar RANLIB=/usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-ranlib ./configure –host=arm-linux –build=x86_64-linux-gnu –prefix=/python

    The ncurses libs status shows as folows,
    checking ncurses.h usability… no
    checking ncurses.h presence… no
    checking for ncurses.h… no

    Can some one help me to build my python with ncurses modules

    Thanks in advance

    • Paul Gibson says:

      Hi yesuarj,

      I would suggest cross-compiling ncurses individually and then pass in CFLAGS and LDFLAGS to the configure to point to the ncurses that you have cross-compiled.

      eg ./configure CFLAGS=/home/yesuarj/myncurses/include LDFLAGS=/home/yesuarj/myncurses/lib

      Paul

  20. [...] [1]:??Cross Compiling Python for Embedded Linux Linux, Python ← Python ??? /* */ [...]

  21. Hiro says:

    hi,Pual.I can’t build _ctypes. Could you give me some help?

    Python build finished, but the necessary bits to build these modules were not found:
    _bsddb _curses _curses_panel
    _sqlite3 _ssl _tkinter
    bsddb185 bz2 dbm
    dl gdbm imageop
    nis readline sunaudiodev
    zlib
    To find the necessary bits, look in setup.py in detect_modules() for the module’s name.

    Failed to build these modules:
    _ctypes

  22. JKN says:

    Thanks for this useful page BTW. A couple of things:

    1) I think you have a typo in the line which runs the python tests. Instead of

    python lib/Python-2.7/test/test___all___.py

    I ran

    python lib/python-2.7/test/test___all___.py # (lower case ‘p’ in ‘python-2.7′)

    Also note that the test filename has three underscores, then two: test_ _ _all_ _.py

    2) I have got Python built for the ARM with sqlite now ;-) . Briefly, the steps are:

    i) download and cross-compile sqlite3 for your target (ie. before building Python)
    ii) make this cross-compiled sqlite3 accessible to Python 2.7.2, by editing setup.py to look for the sqlite3 header in the location you’ve just built it. You seem to have to add it to be the first entry in the list BTW
    iii) build python as per the page above.
    iv) download to target and test. You also have to download the sqlite3 libraries to the target and eg. set LD_LIBRARY_PATH so that these are found.

    BTW, for my Arm target I used

    CC=arm-none-linux-gnueabi-gcc

    instead of

    CC=ppc_6xx-gcc

    you need to make similar changes to the rest of the commands, pretty obviously.

    HTH
    J^n

  23. Bernhard Renz says:

    Ubuntu 11.04 64Bit distribution has the gcc-arm-linux packages. With this packages and the following commands, i got it to work:

    CC=arm-linux-gnueabi-gcc-4.6 CXX=arm-linux-gnueabi-g++ AR=arm-linux-gnueabi-ar RANLIB=arm-linux-gnueabi-ranlib ./configure –host=arm-linux –build=x86_64-linux-gnu –prefix=/python

    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-linux-gnueabi-gcc-4.6 -shared” CROSS_COMPILE=arm-linux-gnueabi- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=x86_64-linux-gnu

    make install HOSTPYTHON=./hostpython BLDSHARED=”arm-linux-gnueabi-gcc-4.6 -shared” CROSS_COMPILE=arm-linux-gnueabi- CROSS_COMPILE_TARGET=yes prefix=~/Python-3.2.2/_install

  24. Justin says:

    Hi Paul. What would be the easiest way to get an embedded Linux on an ARM Cortex M3 so that I can run through this tutorial? Thanks.

  25. Devika Nair says:

    Thanks for the write-up.

    My target is an AM3517 board which needs to run a node.js based webserver.As per the information from the internet, node.js has got dependency with python and libssl-dev.
    Your post helped to cross compile python for arm. Now my problem is to cross compile libssl-dev and node.js for arm.

    Could you help me with this?

  26. Shekhar says:

    Dear Paul,

    AS per the instructoins provided I tried to build the Python 2.7.2 for my ARM target.
    I reached till the applying the patch…

    When i tried for the next command, i,e
    CC=arm-eabi-gcc-4.4.0 CXX=arm-eabi-g++ AR=arm-eabi-ar RANLIB=arm-eabi-ranlib ./configure -–host=arm-linux -–build=x86_32-linux-gnu –-prefix=/python

    I’m getting the below error. Can you please let me know how to resolve this ?

    checking for –enable-universalsdk… no
    checking for –with-universal-archs… 32-bit
    checking MACHDEP… linux3
    checking EXTRAPLATDIR…
    checking machine type as reported by uname -m… i686
    checking for –without-gcc… no
    checking for arm-linux-gcc… arm-eabi-gcc-4.4.0
    checking whether the C compiler works… no
    configure: error: in `/home/rvs/Projects/pythonbuild/Python-2.7.2′:
    configure: error: C compiler cannot create executables
    See `config.log’ for more details

    In my UBUNTU machine I can find all the CC, CXX, AR, RANLIB under this path, but still i’m unable to procced. Please help on this.
    home/rvs/Projects/trunk/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin

    Regards,
    Shekhar

  27. Paul Gibson says:

    Hi Shekhar,

    Try this:

    CC=arm-eabi-gcc-4.4.0 CXX=arm-eabi-g++ AR=arm-eabi-ar RANLIB=arm-eabi-ranlib ./configure -–host=arm-eabi-–build=i386-linux-gnu –-prefix=/python

    Thanks,
    Paul

  28. Shekhar says:

    Hi Paul, I tried the above command, but once again unsuccessful.
    I’m getting the same error i,e

    checking for arm-linux-gcc… arm-eabi-gcc-4.4.0
    checking whether the C compiler works… no
    configure: error: in `/home/rvenkatachar/Projects/pythonbuild/Python-2.7.2?:
    configure: error: C compiler cannot create executables
    See `config.log’ for more details

    #############################################
    for the information I have pasted below the details of the config.log also.

    config log details
    +++++++++++++++++++++++++++++++++

    ## ———– ##
    ## Core tests. ##
    ## ———– ##

    configure:2764: checking for –enable-universalsdk
    configure:2805: result: no
    configure:2814: checking for –with-universal-archs
    configure:2831: result: 32-bit
    configure:2988: checking MACHDEP
    configure:3136: result: linux3
    configure:3142: checking EXTRAPLATDIR
    configure:3157: result:
    configure:3168: checking machine type as reported by uname -m
    configure:3171: result: i686
    configure:3184: checking for –without-gcc
    configure:3228: result: no
    configure:3249: checking for arm-eabi-gcc
    configure:3276: result: arm-eabi-gcc-4.4.0
    configure:3545: checking for C compiler version
    configure:3554: arm-eabi-gcc-4.4.0 –version >&5
    arm-eabi-gcc-4.4.0 (GCC) 4.4.0
    Copyright (C) 2009 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    configure:3565: $? = 0
    configure:3554: arm-eabi-gcc-4.4.0 -v >&5
    Using built-in specs.
    Target: arm-eabi
    Configured with: /home/jingyu/projects/gcc/android-toolchain/gcc-4.4.0/configure –prefix=/usr/local –target=arm-eabi –host=i686-unknown-linux-gnu –build=i686-unknown-linux-gnu –enable-languages=c,c++ –with-gmp=/home/jingyu/projects/gcc/toolchain_build/obj/temp-install –with-mpfr=/home/jingyu/projects/gcc/toolchain_build/obj/temp-install –disable-libssp –enable-threads –disable-nls –disable-libmudflap –disable-libgomp –disable-libstdc__-v3 –disable-sjlj-exceptions –disable-shared –disable-tls –with-float=soft –with-fpu=vfp –with-arch=armv5te –enable-target-optspace –with-abi=aapcs –with-gcc-version=4.4.0 –with-binutils-version=2.19 –with-gmp-version=4.2.4 –with-mpfr-version=2.4.1 –with-gdb-version=6.6 –with-arch=armv5te –with-multilib-list=mthumb-interwork,mandroid –with-sysroot=/g/users/jingyu/toolchain/cupcake_rel_root –program-transform-name=’s&^&arm-eabi-&’
    Thread model: single
    gcc version 4.4.0 (GCC)
    configure:3565: $? = 0
    configure:3554: arm-eabi-gcc-4.4.0 -V >&5
    arm-eabi-gcc-4.4.0: ‘-V’ option must have argument
    configure:3565: $? = 1
    configure:3554: arm-eabi-gcc-4.4.0 -qversion >&5
    arm-eabi-gcc-4.4.0: unrecognized option ‘-qversion’
    arm-eabi-gcc-4.4.0: no input files
    configure:3565: $? = 1
    configure:3585: checking whether the C compiler works
    configure:3607: arm-eabi-gcc-4.4.0 conftest.c >&5
    /home/rvenkatachar/Projects/trunk/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/bin/ld: crt0.o: No such file: No such file or directory
    collect2: ld returned 1 exit status
    configure:3611: $? = 1
    configure:3649: result: no
    configure: failed program was:
    | /* confdefs.h */
    | #define _GNU_SOURCE 1
    | #define _NETBSD_SOURCE 1
    | #define __BSD_VISIBLE 1
    | #define _BSD_TYPES 1
    | #define _DARWIN_C_SOURCE 1
    | #define _XOPEN_SOURCE 600
    | #define _XOPEN_SOURCE_EXTENDED 1
    | #define _POSIX_C_SOURCE 200112L
    | /* end confdefs.h. */
    |
    | int
    | main ()
    | {
    |
    | ;
    | return 0;
    | }
    configure:3654: error: in `/home/rvenkatachar/Projects/pythonbuild/Python-2.7.2′:
    configure:3656: error: C compiler cannot create executables
    See `config.log’ for more details

    ## —————- ##
    ## Cache variables. ##
    ## —————- ##

    ac_cv_env_CC_set=set
    ac_cv_env_CC_value=arm-eabi-gcc-4.4.0
    ac_cv_env_CFLAGS_set=
    ac_cv_env_CFLAGS_value=
    ac_cv_env_CPPFLAGS_set=
    ac_cv_env_CPPFLAGS_value=
    ac_cv_env_CPP_set=
    ac_cv_env_CPP_value=
    ac_cv_env_LDFLAGS_set=
    ac_cv_env_LDFLAGS_value=
    ac_cv_env_LIBS_set=
    ac_cv_env_LIBS_value=
    ac_cv_env_build_alias_set=set
    ac_cv_env_build_alias_value=i386-linux-gnu
    ac_cv_env_host_alias_set=set
    ac_cv_env_host_alias_value=arm-eabi
    ac_cv_env_target_alias_set=
    ac_cv_env_target_alias_value=
    ac_cv_prog_CC=arm-eabi-gcc-4.4.0

    ## —————– ##
    ## Output variables. ##
    ## —————– ##

    AR=’arm-eabi-ar’
    ARCH_RUN_32BIT=”
    ARFLAGS=”
    BASECFLAGS=”
    BLDLIBRARY=”
    BLDSHARED=”
    BUILDEXEEXT=”
    CC=’arm-eabi-gcc-4.4.0′
    CCSHARED=”
    CFLAGS=”
    CFLAGSFORSHARED=”
    CONFIGURE_MACOSX_DEPLOYMENT_TARGET=”
    CONFIG_ARGS=’ ‘\”–host=arm-eabi’\” ‘\”–build=i386-linux-gnu’\” ‘\”-prefix=/scartch/python’\” ‘\”build_alias=i386-linux-gnu’\” ‘\”host_alias=arm-eabi’\” ‘\”CC=arm-eabi-gcc-4.4.0′\”’
    CPP=”
    CPPFLAGS=”
    CXX=’arm-eabi-g++’
    DEFS=”
    DLINCLDIR=”
    DLLLIBRARY=”
    DYNLOADFILE=”
    ECHO_C=”
    ECHO_N=’-n’
    ECHO_T=”
    EGREP=”
    EXEEXT=”
    EXPORT_MACOSX_DEPLOYMENT_TARGET=’#’
    EXTRAMACHDEPPATH=”
    EXTRAPLATDIR=”
    FRAMEWORKALTINSTALLFIRST=”
    FRAMEWORKALTINSTALLLAST=”
    FRAMEWORKINSTALLAPPSPREFIX=”
    FRAMEWORKINSTALLFIRST=”
    FRAMEWORKINSTALLLAST=”
    FRAMEWORKUNIXTOOLSPREFIX=’/scartch/python’
    GNULD=”
    GREP=”
    HAS_HG=”
    HAVE_GETHOSTBYNAME=”
    HAVE_GETHOSTBYNAME_R=”
    HAVE_GETHOSTBYNAME_R_3_ARG=”
    HAVE_GETHOSTBYNAME_R_5_ARG=”
    HAVE_GETHOSTBYNAME_R_6_ARG=”
    HGBRANCH=”
    HGTAG=”
    HGVERSION=”
    INSTALL_DATA=”
    INSTALL_PROGRAM=”
    INSTALL_SCRIPT=”
    INSTSONAME=”
    LDCXXSHARED=”
    LDFLAGS=”
    LDLAST=”
    LDLIBRARY=”
    LDLIBRARYDIR=”
    LDSHARED=”
    LIBC=”
    LIBFFI_INCLUDEDIR=”
    LIBM=”
    LIBOBJS=”
    LIBRARY=”
    LIBS=”
    LIBTOOL_CRUFT=”
    LINKCC=”
    LINKFORSHARED=”
    LIPO_32BIT_FLAGS=”
    LN=”
    LTLIBOBJS=”
    MACHDEP=’linux3′
    MACHDEP_OBJS=”
    MAINCC=”
    OBJEXT=”
    OPT=”
    OTHER_LIBTOOL_OPT=”
    PACKAGE_BUGREPORT=’http://bugs.python.org/’
    PACKAGE_NAME=’python’
    PACKAGE_STRING=’python 2.7′
    PACKAGE_TARNAME=’python’
    PACKAGE_URL=”
    PACKAGE_VERSION=’2.7′
    PATH_SEPARATOR=’:’
    PKG_CONFIG=”
    PYTHONFRAMEWORK=”
    PYTHONFRAMEWORKDIR=’no-framework’
    PYTHONFRAMEWORKIDENTIFIER=’org.python.python’
    PYTHONFRAMEWORKINSTALLDIR=”
    PYTHONFRAMEWORKPREFIX=”
    RANLIB=’arm-eabi-ranlib’
    RUNSHARED=”
    SGI_ABI=”
    SHELL=’/bin/bash’
    SHLIBS=”
    SIGNAL_OBJS=”
    SO=”
    SOVERSION=’1.0′
    SRCDIRS=”
    SVNVERSION=”
    THREADHEADERS=”
    THREADOBJ=”
    TRUE=”
    UNICODE_OBJS=”
    UNIVERSALSDK=”
    UNIVERSAL_ARCH_FLAGS=”
    USE_SIGNAL_MODULE=”
    USE_THREAD_MODULE=”
    VERSION=’2.7′
    ac_ct_CC=”
    bindir=’${exec_prefix}/bin’
    build_alias=’i386-linux-gnu’
    datadir=’${datarootdir}’
    datarootdir=’${prefix}/share’
    docdir=’${datarootdir}/doc/${PACKAGE_TARNAME}’
    dvidir=’${docdir}’
    exec_prefix=’NONE’
    host_alias=’arm-eabi’
    htmldir=’${docdir}’
    includedir=’${prefix}/include’
    infodir=’${datarootdir}/info’
    libdir=’${exec_prefix}/lib’
    libexecdir=’${exec_prefix}/libexec’
    localedir=’${datarootdir}/locale’
    localstatedir=’${prefix}/var’
    mandir=’${datarootdir}/man’
    oldincludedir=’/usr/include’
    pdfdir=’${docdir}’
    prefix=’/scartch/python’
    program_transform_name=’s,x,x,’
    psdir=’${docdir}’
    sbindir=’${exec_prefix}/sbin’
    sharedstatedir=’${prefix}/com’
    sysconfdir=’${prefix}/etc’
    target_alias=”

    ## ———– ##
    ## confdefs.h. ##
    ## ———– ##

    /* confdefs.h */
    #define _GNU_SOURCE 1
    #define _NETBSD_SOURCE 1
    #define __BSD_VISIBLE 1
    #define _BSD_TYPES 1
    #define _DARWIN_C_SOURCE 1
    #define _XOPEN_SOURCE 600
    #define _XOPEN_SOURCE_EXTENDED 1
    #define _POSIX_C_SOURCE 200112L

    configure: exit 77
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Please let me know how to proceed ?

    Regards,
    Shekhar

    • Paul Gibson says:

      Hi Shekhar,

      Thanks for the detailed log file. The problem appears to be this line:
      /home/rvenkatachar/Projects/trunk/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/bin/ld: crt0.o: No such file: No such file or directory

      There seems to be something wrong with your toolchain as ld isn’t finding crt0.o. Can you try running ld manually and see if it works?

      Paul

      • Shekhar says:

        Hi Paul,

        As suggested by you , there was problem in the earlier toolchain what I was using.
        So I successfully built the python 2.7.2 with the different toolchain by using the below different commands.

        CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++ AR=arm-none-linux-gnueabi-ar RANLIB=arm-none-linux-gnueabi-ranlib ./configure -–host=arm-none-linux -–build=x86_64-linux-gnu –prefix=/python

        make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-none-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-none-linux-gnueabi- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-none-linux BUILDARCH=x86_64-linux-gnu

        make install HOSTPYTHON=./hostpython BLDSHARED=”arm-none-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-none-linux-gnueabi- CROSS_COMPILE_TARGET=yes prefix=~/raj/_install

        I copied the entire _install directory to the target. (in my case the space was not an issue -as we boot the device using nfs mounted).

        In the target, I set the path of the python to /_install/bin.
        Also, I set the PYTHONHOME environment variable to nfs mounted drive i,e — someip:/home//_install

        When I booted the device, and try to execute python on target — it says ==> python not found.
        Can you please let me know how to proceed ?

        Regards,
        shekhar

        • Paul Gibson says:

          Hi Shekhar,

          Could you show me how you are running python? Are you running python from /_install/bin? Is it on the PATH?

          Paul

          • Shekhar says:

            hi Paul,

            Thanks for the reply.

            I have copied the entire built python directory to my target device under /system/usr
            I have set the path environment variable like this …
            export PATH=$PATH:/system/usr

            My target is connected to my Ubuntu machine(10.04) and able to communicate to target over serial port using minicom. Using minicom, i have opened the shell prompt of the device.
            In shell prompt, I’m trying to execute python.
            If I execute the python command, I’m getting python not found !!!

            Below is the snapshot of the error
            ==========================================
            root@android:/# echo $PATH
            /sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin:/system/usr
            root@android:/# python
            /system/bin/sh: python: not found
            127|root@android:/# cd /system/usr/_install/bin
            root@android:/system/usr/_install/bin# python
            /system/bin/sh: python: not found
            =============================================

            Do I need set any other environment variables, or do I need to copy any other lib files from host to target.
            Please let me know.

            For your info : My target device has the Android’s Ice Cream Sandwitch OS running.

            Regards,
            Shekhar

            • Paul Gibson says:

              Hi Shekhar,

              My first guess is that you are still missing libraries that python is looking for:
              /system/bin/sh: python: not found

              The Android root filesystem is not standard Linux and therefore I cannot help. You may want to look at other articles such as:
              http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/

              Paul

              • Shekhar says:

                hi Paul,

                Thanks for the help provided….
                Now I’m looking into the link provided by you [http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/]. I have done good progress in compiling the Python for the Android.

                Thanks for the help ….

                Regards,
                Shekhar

                • Shekhar says:

                  hi Paul,

                  Thanks for the link , [http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/]. I successfully built python for Android. I ported into my target board….I can execute few python commands like, python -V and python –help.

                  But, python prompt is not coming on target..and idea why it is so.. any tricks for working ??
                  Also, I wrote simple python script which imports os, random and time modules….
                  Seems only os module is working but others are not working,,,can you please suggest some ideas what do i need to do to make it work??

                  I have set the environment variables too..
                  PYHTHONHOME = /system/lib/python2.7
                  PYHTHONPATH = /system/lib/python2.7

                  Regards,
                  Rajashekhar

  29. sudhesh says:

    Hi Paul,

    Thanks for you cross-compiling-python-for-embedded-linux blog.
    I want to run the Python on ARM QEMU(Hardware emulator).

    I have successfully cross compiled the Python2.7.2 for ARM QEMU and I have got the below build directories
    /bin /include /lib /share

    and I have copied all the Library files from /lib/python2.7/* to target(QEMU) /usr/lib/* directory.
    and /bin/python to target /usr/bin/python
    and i ran below commands on target:

    # export PYTHONPATH=/usr/bin/python
    # export PYTHONHOME=/usr/lib/

    # python -V
    python: error while loading shared libraries: libpthread.so.0: cannot open shared object file: No such file or directory

    “then I have copied libpthread.so.0 file from HOST arm-tool-chain “arm-none-linux-gnueabi/libc/lib/libpthread.so.0″ to /usr/lib/* on target.

    Now am getting the below error:

    # python -V
    python: relocation error: /usr/lib/libpthread.so.0: symbol __default_sa_restorer_v2, version GLIBC_PRIVATE not defined in file libc.so.6 with link time reference

    Could you please guide me the steps to bring the build files to the target and running the Python on the target.

    Regards,
    Sudhesh

  30. Paul Gibson says:

    Hi Sudhesh,

    It could be one of two things:
    – an issue with how you have copied your toolchain libraries to the target device.
    – Your Python build is built against a different version of libpthread than what is installed on the device.

    For copying the files to target:
    – Copy all the libraries from arm-none-linux-gnueabi/libc/lib/ to /lib on the target.
    – Copy all the libraries from arm-none-linux-gnueabi/libc/usr/lib/ to /usr/lib on the target.

    When building Python, make sure that you are building against the same libraries as provided by the toolchain.

    Paul

    • sudhesh says:

      Hi Paul,

      Thank you for advise, I have copied all the lib file from arm-none-linux-gnueabi/libc/lib/ to /lib
      and host _install/bin/* //usr/bin/*
      I have not found arm-none-linux-gnueabi/libc/usr/lib/ to copy /usr/lib on the target.

      Now Python is running on the target, but getting error while executing the sample scripts.

      # python -V
      Python 2.6.6

      # python
      ‘import site’ failed; use -v for traceback
      Python 2.6.6 (r266:84292, Apr 1 2012, 12:06:25)
      [GCC 4.4.1] on linux2
      Type “help”, “copyright”, “credits” or “license” for more information.
      >>>

      # cat test.py
      import os
      print “Well here we are again”
      os.system(‘ls -lt’)

      # python2.6 test.py
      ‘import site’ failed; use -v for traceback
      Traceback (most recent call last):
      File “test.py”, line 1, in
      import os
      ImportError: No module named os

      # python2.6 -v test.py
      # installing zipimport hook
      import zipimport # builtin
      # installed zipimport hook
      ‘import site’ failed; traceback:
      ImportError: No module named site
      Python 2.6.6 (r266:84292, Apr 1 2012, 12:06:25)
      [GCC 4.4.1] on linux2
      Type “help”, “copyright”, “credits” or “license” for more information.
      Traceback (most recent call last):
      File “test.py”, line 1, in
      import os
      ImportError: No module named os
      # clear __builtin__._
      # clear sys.path
      # clear sys.argv
      # clear sys.ps1
      # clear sys.ps2
      # clear sys.exitfunc
      # clear sys.exc_type
      # clear sys.exc_value
      # clear sys.exc_traceback
      # clear sys.last_type
      # clear sys.last_value
      # clear sys.last_traceback
      # clear sys.path_hooks
      # clear sys.path_importer_cache
      # clear sys.meta_path
      # clear sys.flags
      # clear sys.float_info
      # restore sys.stdin
      # restore sys.stdout
      # restore sys.stderr# python2.6 -v test.py
      # installing zipimport hook
      import zipimport # builtin
      # installed zipimport hook
      ‘import site’ failed; traceback:
      ImportError: No module named site
      Python 2.6.6 (r266:84292, Apr 1 2012, 12:06:25)
      [GCC 4.4.1] on linux2
      Type “help”, “copyright”, “credits” or “license” for more information.
      Traceback (most recent call last):
      File “test.py”, line 1, in
      import os
      ImportError: No module named os
      # clear __builtin__._
      # clear sys.path
      # clear sys.argv
      # clear sys.ps1
      # clear sys.ps2
      # clear sys.exitfunc
      # clear sys.exc_type
      # clear sys.exc_value
      # clear sys.exc_traceback
      # clear sys.last_type
      # clear sys.last_value
      # clear sys.last_traceback
      # clear sys.path_hooks
      # clear sys.path_importer_cache
      # clear sys.meta_path
      # clear sys.flags
      # clear sys.float_info
      # restore sys.stdin
      # restore sys.stdout
      # restore sys.stderr
      # cleanup __main__
      # cleanup[1] zipimport
      # cleanup[1] signal
      # cleanup[1] exceptions
      # cleanup[1] _warnings
      # cleanup sys
      # cleanup __builtin__
      # cleanup ints: 3 unfreed ints
      # cleanup floats
      #

      # cleanup __main__
      # cleanup[1] zipimport
      # cleanup[1] signal
      # cleanup[1] exceptions
      # cleanup[1] _warnings
      # cleanup sys
      # cleanup __builtin__
      # cleanup ints: 3 unfreed ints
      # cleanup floats
      #

      Could please guide me what’s went wrong ?

  31. Rudolf says:

    Hi, all

    Trying to follow the tutorial. Compile on Ubuntu 10.04 32-bit for ARM CPU (not for android)
    Config part works:
    CC=arm-cdcs-linux-gnueabi-gcc CXX=arm-cdcs-linux-gnueabi-g++ AR=arm-cdcs-linux-gnueabi-ranlib ./configure –host=arm-linux –build=x86-linux-gnu –prefix=/python

    Now for building:
    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-cdcs-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-cdcs-linux-gnueabi- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=x86-linux-gnu

    And I am getting an error (including few lines just before the error):
    arm-cdcs-linux-gnueabi-gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -c ./Modules/xxsubtype.c -o Modules/xxsubtype.o
    arm-cdcs-linux-gnueabi-gcc -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE \
    -DSVNVERSION=”\”`LC_ALL=C svnversion .`\”" \
    -DHGVERSION=”\”`LC_ALL=C `\”" \
    -DHGTAG=”\”`LC_ALL=C `\”" \
    -DHGBRANCH=”\”`LC_ALL=C `\”" \
    -o Modules/getbuildinfo.o ./Modules/getbuildinfo.c
    rm -f libpython2.7.a
    arm-cdcs-linux-gnueabi-ranlib rc libpython2.7.a Modules/getbuildinfo.o
    arm-cdcs-linux-gnueabi-ranlib: ‘rc’: No such file
    arm-cdcs-linux-gnueabi-ranlib: ‘libpython2.7.a’: No such file
    arm-cdcs-linux-gnueabi-ranlib: Modules/getbuildinfo.o: File format not recognized

    Modules/getbuildinfo.o is there and is of ARM ELF file format.
    It also looks like libpython2,7.a is removed before being used????

    Thanks,
    RUdolf
    make: *** [libpython2.7.a] Error 1

Leave a Reply

(required)