Python Chinese Community The spiritual tribe of Python Chinese developers around the world.jpg”)

The author uses C language development in ARM-LINUX embedded platform for a long time. Hardware IO operations can only be done with C, but the application is simply helpless and painful, the program is a bit complicated, various overboundary, pointer error, weird crash, segment fault, memory leak, core dump, compile can not find the file, rely on the library, parsing a character takes a long time, too few wheel purely rely on scratch. Since porting Python to an embedded platform, writing an IO Python extension library in C and then writing an application in Python has been completely destructive.

  • Python version: 2.7.3
  • Cross-compiler: arm-none-linux-gnueabi-
  • Hardware platform: AT91 ARM9, NUC97x, TI AM335x

Source code download & decompression

Copy the code
  1. Wget HTTP: / / https://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.xz
  2. Xz - d Python - 2.7.3. Tar. Xz
  3. The tar XVF Python - 2.7.3. Tar

Compile the HOST interpreter

Compiling the embedded version of Python requires the interpreter to parse setup.py to compile Python modules, so the HOST interpreter needs to be compiled first.

Copy the code
  1. ./configure  
  2. make python Parser/pgen  
  3. mv  python  hostpython  
  4. mv  Parser/pgen  Parser/hostpgen  
  5. make distclean

Cross-compile patches

Copy the code
  1. Patch - p1 < Python - 2.7.3 - xcompile. Patch

Cross-compile configuration

Set the cross-compilation tool chain to arm-none-linux-gnueabi, and store the generated execution files in the _install folder of the current directory.

Copy the code
  1. ./configure --host=arm-none-linux-gnueabi --prefix=$PWD/_install

Compile & Install

Copy the code
  1. make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED="arm-none-linux-gnueabi-gcc -shared" CROSS_COMPILE=arm-none-linux-gnueabi- CROSS_COMPILE_TARGET=yes  
  2. make install HOSTPYTHON=./hostpython BLDSHARED="arm-none-linux-gnueabi-gcc-shared" CROSS_COMPILE=arm-none-linux-gnueabi- CROSS_COMPILE_TARGET=yes prefix=$PWD/_install

After executing the preceding command, generate four folders of bin lib include share in _install. To avoid trouble, you can write the preceding command as a script.

Target board file copy

Copy everything in _install/bin to a directory that any environment variable on the target board can access, such as /bin and /usr/bin.

Copy everything in _install/lib to the target board /lib

Copy everything in _install /include to the target board /include. Because some of these headers are required by the Python environment, such as the Python interpreter startup dependent on Pyconfig. h and the import time module dependent on TimeFuncs.h.

Setting environment variables

Add the PYTHON2.7 path to the environment variables PYTHONHOME, PYTHONPATH.

For /etc/profile, add the following information to the end of the file and run source /etc/profile.

Copy the code
  1. Export PYTHONPATH = / lib/python2.7: $PYTHONPATH
  2. Export PYTHONHOME = / lib/python2.7: $PYTHONHOME

For command lines, run the following command:

Copy the code
  1. Export PYTHONPATH = $PYTHONPATH: / lib/python2.7
  2. Export PYTHONHOME = $PYTHONHOME: / lib/python2.7

Verify the target board Python

Open the interpreter, import some common modules, write programs to test.

Afterword.

It is possible to import time, datetime, threading, and multiprocessing to indicate that the time, _collections, and itertools modules are missing.

These are builtin modules, which can be viewed through sys. builtinModulenames. The source code is in Modules, compiled as.so in /lib/python2.7/lib-dynload.

First check whether disabledModulelist =[] disables these modules in setup.py.

If you still can’t solve the problem, add code to Modules/config.c by referring to other Modules and compile it manually to create the.so file in the build/lib.xx.2.7 directory.

\

Xiang An is an embedded Linux and Python expert and currently focuses on the integration of Python and the Internet of Things. He loves technical writing and is the author of The Book “Practical Python Development for the Internet of Things”.



\

\

Appreciate the author

Recent Hot articles

Learn more about wechat friends \ with Python

How to be a dirty programmer in Python \

New discovery of using Python to crawl 100,000 comments on Eason Chan’s new song “We” \

Using Python to analyze Apple stock price data \

Python Natural Language Processing analysis of heaven and dragon

Click **** to read the original article and become a free member of **** community