Cross-compiling Python for MIPS and ARM Platforms

Python programming language is used in several open source projects such as Sugar OS and Xibo. Let’s see if we can cross-compile it in Ubuntu 10.10 using a mips compiler.

I’ll use the instructions given at http://randomsplat.com/id5-cross-compiling-python-for-embedded-linux.html.

Let’s download Python 2.7.1 first and extract the source code:


Then run the following command in Python-2.7.1 in order to build some tools for the host:


There is no patch for Python 2.7.1 cross-compilation in the link above, so let’s just try to configure and build it:


If we don’t use a patch the first error is:


So I used some older patch to create a new patch: http://www.cnx-software.com/patch/python-2.7.1-cross-compile.patch. You can download it an apply it as follows:


And repeat the step above to configure and cross-compile Python for mips.

Finally install Python in ~/Python-2.7.1/install for example:


After that copy all necessary files in ~/Python-2.7.1/install to your target board and run the python test:

Share this:
FacebookTwitterHacker NewsSlashdotRedditLinkedInPinterestFlipboardMeWeLineEmailShare

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK 5 ITX RK3588 mini-ITX motherboard

23 Replies to “Cross-compiling Python for MIPS and ARM Platforms”

    1. You need to change that part:

      CC=mipsel-linux-gcc CXX=mipsel-linux-g++ AR=mipsel-linux-ar RANLIB=mipsel-linux-ranlib ./configure –host=mipsel-linux –target=mipsel-linux –prefix=/python

      make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=mipsel-linux- CROSS_COMPILE_TARGET=yes

      But it depends on the name of your ARM cross-compiler. If you are using Linaro gcc: arm-linux-gnueabi-gcc, then it would be something like:

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

      make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=arm-linux-gnueabi- CROSS_COMPILE_TARGET=yes

  1. And how add support for dbus and glib to python running on arm ? I made this procedure and everything works just fine but I need also dbus python module, do you have any guide how to do it ?

    1. Sorry, I haven’t done this. But the code for dbus and glib can be downloaded so I suppose it should not be a problem.
      I’ve checked the code at http://dbus.freedesktop.org/releases/dbus-python/
      It seems you can just do something like:

      CC=mipsel-linux-gcc CXX=mipsel-linux-g++ AR=mipsel-linux-ar RANLIB=mipsel-linux-ranlib ./configure –host=mipsel-linux –target=mipsel-linux –prefix=/python
      make
      make install

  2. @cnxsoft
    for arm, make file created successfully without errors. But when i make by using the command
    make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-none-linux-gnueabi-gcc -shared” CROSS_COMPILE=arm-none-linux- CROSS_COMPILE_TARGET=yes

    i got the error as

    arm-none-linux-gnueabi-gcc -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Modules/python.o ./Modules/python.c
    In file included from Include/Python.h:58:0,
    from ./Modules/python.c:3:
    Include/pyport.h:243:13: error: #error “This platform’s pyconfig.h needs to define PY_FORMAT_LONG_LONG”
    make: *** [Modules/python.o] Error 1

    Help me to solve the issue..

  3. It successfully created make file for arm, But while make by using the command

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

    i get the error as

    arm-none-linux-gnueabi-gcc -Xlinker -export-dynamic -o python \
    Modules/python.o \
    libpython2.7.a -lpthread -ldl -lpthread -lutil -lm
    File “./setup.py”, line 316
    self.announce(‘*** WARNING: renaming “%s” since importing it’
    ^
    IndentationError: expected an indented block
    make: *** [sharedmods] Error 1

    Help me to solve..

  4. To cross compiling Python 3.5 for arm, what the patch i have to use ? or any other process to cross compile.
    Suggest me

  5. Hi all,

    when I’m executing this command “CC=mipsel-linux-gcc CXX=mipsel-linux-g++ AR=mipsel-linux-ar RANLIB=mipsel-linux-ranlib ./configure –host=mipsel-linux –target=mipsel-linux –prefix=/python” I’m getting the following error message:
    “configure: error: invalid variable name: `–host'” Do you have any idea whats causing this?

    Thank you

  6. @Steven
    Sorry there was some problems with the website platform (WordPress) .
    where double hyphen

    would show as a single hyphen

    I’ve now updated the post to fix the issue. The instructions are old so they may not work with the latest version Python, but should still work with Python 2.7.

  7. When i am running this command

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

    getting the error as

    /usr/bin/install -c -m 644 ./LICENSE /home/arm_install/lib/python2.7/LICENSE.txt
    PYTHONPATH=/home/arm_install/lib/python2.7 \
    ./hostpython -Wi -tt /home/arm_install/lib/python2.7/compileall.py \
    -d /home/arm_install/lib/python2.7 -f \
    -x ‘bad_coding|badsyntax|site-packages|lib2to3/tests/data’ \
    /home/arm_install/lib/python2.7
    Traceback (most recent call last):
    File “/home/arm_install/lib/python2.7/compileall.py”, line 17, in
    import struct
    File “/home/arm_install/lib/python2.7/struct.py”, line 1, in
    from _struct import *
    ImportError: /u02/python_crosscompile/Python-2.7.1/build/lib.linux-x86_64-2.7/_struct.so: wrong ELF class: ELFCLASS32
    make: *** [libinstall] Error 1

    what is this ? How to solve.

    1. ake install HOSTPYTHON=./hostpython BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=mipsel-linux- CROSS_COMPILE_TARGET=yes prefix=/home/liucong/Python-2.7.1/install
      Parser/pgen ./Grammar/Grammar ./Include/graminit.h ./Python/graminit.c
      Parser/pgen: Parser/pgen: cannot execute binary file
      make: [Include/graminit.h] Error 126 (ignored)
      Parser/pgen ./Grammar/Grammar ./Include/graminit.h ./Python/graminit.c
      Parser/pgen: Parser/pgen: cannot execute binary file
      make: [Python/graminit.c] Error 126 (ignored)
      /usr/bin/install -c python /home/liucong/Python-2.7.1/install/bin/python2.7
      if test -f libpython2.7.a; then \
      if test -n “” ; then \
      /usr/bin/install -c -m 555 /home/liucong/Python-2.7.1/install/bin; \
      else \
      /usr/bin/install -c -m 555 libpython2.7.a /home/liucong/Python-2.7.1/install/lib/libpython2.7.a; \
      if test libpython2.7.a != libpython2.7.a; then \
      (cd /home/liucong/Python-2.7.1/install/lib; ln -sf libpython2.7.a libpython2.7.a) \
      fi \
      fi; \
      else true; \
      fi
      File “./setup.py”, line 316
      self.announce(‘*** WARNING: renaming “%s” since importing it’
      ^
      IndentationError: expected an indented block
      make: *** [sharedmods] Error 1
      [liucong@centos Python-2.7.1]$ vim setup.py
      [liucong@centos Python-2.7.1]$ uname -a
      Linux centos 2.6.32-696.3.1.el6.x86_64 #1 SMP Tue May 30 19:52:55 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
      [liucong@centos Python-2.7.1]$
      how to solve this problem,thanks

          1. Yes.. I should have paid more attention to the log.
            The OP should look at Parser/pgen with file or other tools.

          2. I want to cross compile Python for mips on x64 running centos 6, how to solve this problem or what should I do?thanks

          3. You need to solve this issue:
            “Parser/pgen: Parser/pgen: cannot execute binary file”

            Analyze the file with “file” utility, and possibly run the make command in verbose mode:

          4. today,I trying . I found this problem to occur after the command was executed.this problem stills exists
            make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”mipsel-linux-gcc -shared” CROSS_COMPILE=mipsel-linux- CROSS_COMPILE_TARGET=yes

            configure: WARNING: If you wanted to set the –build type, don’t use –host.
            If a cross compiler is detected then cross compile mode will be used.
            checking for –enable-universalsdk… no
            checking for –with-universal-archs… 32-bit
            checking MACHDEP… linux2
            checking EXTRAPLATDIR…
            checking machine type as reported by uname -m… x86_64
            checking for –without-gcc… no
            checking for mipsel-linux-gcc… mipsel-linux-gcc
            checking whether the C compiler works… yes
            checking for C compiler default output file name… a.out
            checking for suffix of executables…
            checking whether we are cross compiling… yes
            checking for suffix of object files… o
            checking whether we are using the GNU C compiler… yes
            checking whether mipsel-linux-gcc accepts -g… yes
            checking for mipsel-linux-gcc option to accept ISO C89… none needed
            checking for –with-cxx-main=… no
            ……………………
            mipsel-linux-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 echo Unversioned directory\”” -o Modules/getbuildinfo.o ./Modules/getbuildinfo.c
            rm -f libpython2.7.a
            mipsel-linux-ar rc libpython2.7.a Modules/getbuildinfo.o
            mipsel-linux-ar rc libpython2.7.a Parser/acceler.o Parser/grammar1.o Parser/listnode.o Parser/node.o Parser/parser.o Parser/parsetok.o Parser/bitset.o Parser/metagrammar.o Parser/firstsets.o Parser/grammar.o Parser/pgen.o Parser/myreadline.o Parser/tokenizer.o
            mipsel-linux-ar rc libpython2.7.a Objects/abstract.o Objects/boolobject.o Objects/bufferobject.o Objects/bytes_methods.o Objects/bytearrayobject.o Objects/capsule.o Objects/cellobject.o Objects/classobject.o Objects/cobject.o Objects/codeobject.o Objects/complexobject.o Objects/descrobject.o Objects/enumobject.o Objects/exceptions.o Objects/genobject.o Objects/fileobject.o Objects/floatobject.o Objects/frameobject.o Objects/funcobject.o Objects/intobject.o Objects/iterobject.o Objects/listobject.o Objects/longobject.o Objects/dictobject.o Objects/memoryobject.o Objects/methodobject.o Objects/moduleobject.o Objects/object.o Objects/obmalloc.o Objects/rangeobject.o Objects/setobject.o Objects/sliceobject.o Objects/stringobject.o Objects/structseq.o Objects/tupleobject.o Objects/typeobject.o Objects/weakrefobject.o Objects/unicodeobject.o Objects/unicodectype.o
            mipsel-linux-ar rc libpython2.7.a Python/_warnings.o Python/Python-ast.o Python/asdl.o Python/ast.o Python/bltinmodule.o Python/ceval.o Python/compile.o Python/codecs.o Python/errors.o Python/frozen.o Python/frozenmain.o Python/future.o Python/getargs.o Python/getcompiler.o Python/getcopyright.o Python/getplatform.o Python/getversion.o Python/graminit.o Python/import.o Python/importdl.o Python/marshal.o Python/modsupport.o Python/mystrtoul.o Python/mysnprintf.o Python/peephole.o Python/pyarena.o Python/pyctype.o Python/pyfpe.o Python/pymath.o Python/pystate.o Python/pythonrun.o Python/structmember.o Python/symtable.o Python/sysmodule.o Python/traceback.o Python/getopt.o Python/pystrcmp.o Python/pystrtod.o Python/dtoa.o Python/formatter_unicode.o Python/formatter_string.o Python/dynload_shlib.o Python/thread.o
            mipsel-linux-ar rc libpython2.7.a Modules/config.o Modules/getpath.o Modules/main.o Modules/gcmodule.o
            mipsel-linux-ar rc libpython2.7.a Modules/threadmodule.o Modules/signalmodule.o Modules/posixmodule.o Modules/errnomodule.o Modules/pwdmodule.o Modules/_sre.o Modules/_codecsmodule.o Modules/_weakref.o Modules/zipimport.o Modules/symtablemodule.o Modules/xxsubtype.o
            mipsel-linux-ranlib libpython2.7.a
            mipsel-linux-gcc -Xlinker -export-dynamic -o python \
            Modules/python.o \
            libpython2.7.a -lpthread -ldl -lpthread -lutil -lm
            libpython2.7.a(posixmodule.o): In function posix_tmpnam':
            /home/liucong/Python-2.7.1/./Modules/posixmodule.c:7346: warning: the use of
            tmpnam_r’ is dangerous, better use mkstemp'
            libpython2.7.a(posixmodule.o): In function
            posix_tempnam’:
            /home/liucong/Python-2.7.1/./Modules/posixmodule.c:7301: warning: the use of tempnam' is dangerous, better usemkstemp’
            File “./setup.py”, line 316
            self.announce(‘*** WARNING: renaming “%s” since importing it’
            ^
            IndentationError: expected an indented block
            make: *** [sharedmods] Error 1

          5. I suspect that this “pgen” utility was built with the cross-compiler instead of being built with your local CC. I’m seeing a HOSTPGEN variable in your build command that makes me think that this utility ought to have been used instead. You should carefully review the makefile to be sure that your variables are properly named and that the resulting utility is usable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Boardcon Rockchip and Allwinner SoM and SBC products
Boardcon Rockchip and Allwinner SoM and SBC products