How to Port Python on iMX6Q Development Board
This article is based on Forlinx OKMX6Q/ OKMX6DL-C development board. This series uses i.MX6Q/DL processors. The system for demonstrating Python porting is linux3.0.35. Other platforms can refer it, but there will be differences among different platforms, you may modify according to the actual circumstance.
iMX6Q/DL Python Porting
Attention: Required toolkits are in Python porting/toolkits
Generated Portable Python is in Python porting/output
Process 1: Compile Python
Preparatory work
The following files are needed in the following compilation
Python-2.7.13.tgz
sqlite-autoconf-3200100.tar.gz
Python-2.7.13-xcompile.patch.tar.gz
ntp-4.2.8p10.tar
setuptools-0.6c11-py2.7.egg
openssl-OpenSSL_1_0_1b.zip
zlib-1.2.3.tar.g
Prepare the Environment
cd /root
mkdir mx6q-python2.7/ && cd mx6q-python2.7
mkdir –p {python2_7_13_for_x86_64,python2_7_13_for_arm,INSTALL,sqlite3,misc,output/sources}
Copy the package prepared above to mx6q-python2.7, and move zlib-1.2.3.tar.gz and openssl-OpenSSL_1_0_1b.zip to directory: misc. Or directly copy the file mx6q-python2.7 in the provided package to directory: /root .
View the cross-compiler toolchain
View the current cross-compiler toolchain. If inconsistent, replace with the compiler below.
The current cross-compiler toolchain is arm-none-linux-gnueabi-, which can be viewed by the following command.
find / -name arm-*-gcc
If the cross-compiler toolchain is inconsistent, replace with the compiler below
Compile and install
Compile and install sqlite3
Compile and install sqlite3, Python needs sqlite3 support.
cd /root/mx6q-python2.7/
tar xvf sqlite-autoconf-3200100.tar.gz
cd sqlite-autoconf-3200100
./configure --host=arm-none-linux-gnueabi --prefix=$PWD/../sqlite3
make -j8 && make install
Compile openssl and zlib
compile zlib
cd /root/mx6q-python2.7/misc/
tar xvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
mkdir arm_install
./configure --prefix=./arm_install/
sed -i 's/gcc/arm-none-linux-gnueabi-gcc/g' Makefile
sed -i 's/AR=ar/AR=arm-none-linux-gnueabi-ar/g' Makefile
make
make install
Compile openssl
cd /root/mx6q-python2.7/misc/
unzip openssl-OpenSSL_1_0_1b.zip
cd openssl-OpenSSL_1_0_1b
mkdir arm_install
./Configure --prefix=`pwd`/arm_install/ os/compiler:arm-none-linux-gnueabi-gcc
modify the CC variable in Makefile to CC= $(CROSS_COMPILE)gcc
make
make install
Package the compiled installation package, which will be used when porting to the development board
cd arm_install
tar jcvf openssl-OpenSSL_1_0_1b.tar.bz2 *
cp openssl-OpenSSL_1_0_1b.tar.bz2 /root/mx6q-python2.7/output/sources
| Compile python2.7
Compile python2.7 for x86
cd /root/mx6q-python2.7/
tar xvf Python-2.7.13.tgz
cd python2_7_13_for_x86_64/
sed -i 's/self.extensions.remove(ext)/print(ext.name)/g' ../Python-2.7.13/setup.py
../Python-2.7.13/configure --prefix=`pwd`
make -j8 && make install
Compile python2.7 for arm
Patch
cd ..
tar xvf Python-2.7.13-xcompile.patch.tar.gz -C Python-2.7.13
cd Python-2.7.13/
patch -p1 < Python-2.7.13-xcompile.patch
Modify the configuration of module connection
vim Modules/Setup.dist, uncomment the following configuration
zlib zlibmodule.c -I$(prefix)/include -L$(prefix)/lib -lz
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
Compile python2.7 for arm
configure
cd ../python2_7_13_for_arm/
Copy bksh to /root/mx6q-python2.7/
cp ../bksh/py2* . can directly run the script py2config-for-arm.sh
Or enter the command below.
../Python-2.7.13/configure --prefix=`pwd` \
--host=arm-none-linux-gnueabi \
--build=x86_64-linux-gnu \
--enable-ipv6 \
--enable-static \
ac_cv_file__dev_ptmx="yes" \
ac_cv_file__dev_ptc="no" \
LDFLAGS="-L$PWD/../sqlite3/lib -L$PWD/lib" \
CPPFLAGS="-I$PWD/../sqlite3/include -I$PWD/include -I$PWD/include/openssl"
Before the next step: execute make, install zlib and openssl. And copy each respective include and lib to python2_7_13_for_arm. Or directly run py2-cp-libAndssl-arm-toHere.sh to automatically copy the include and lib of the zlib and openssl under misc to the current directory.
Compile
cd /root/mx6q-python2.7/python2_7_13_for_arm/
Run py2make-for-arm.sh or run the command below.
make install HOSTPYTHON=../python2_7_13_for_x86_64/python \
BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \
CROSS_COMPILE=arm-none-linux-gnueabi- \
CROSS_COMPILE_TARGET=yes \
prefix=$PWD/../INSTALL
Install
Run py2install.sh or run the command below.
make install HOSTPYTHON=../python2_7_13_for_x86_64/python \
BLDSHARED="arm-none-linux-gnueabi-gcc -shared" \
CROSS_COMPILE=arm-none-linux-gnueabi- \
CROSS_COMPILE_TARGET=yes \
prefix=$PWD/../INSTALL
Package
cd ../INSTALL/
tar jcvf python2.7-for-arm.tar.bz2 *
cp python2.7-for-arm.tar.bz2 ../output/sources/
| ntpdate network time synchronization tool
cross compile
cd /root/mx6q-python2.7
tar xvf ntp-4.2.8p10.tar.gz
cd ntp-4.2.8p10/
mkdir arm_install
./configure --prefix=$PWD/arm_install --host=arm-none-linux-gnueabi --enable-static --with-yielding-select=yes
make && make install
Package
If do not need help documents, delete the directory /share in arm_install, to save space
cd arm_install
rm share -rf
tar jcvf ntp-4.2.8.p10.tar.bz2 *
cp ntp-4.2.8.p10.tar.bz2 ../../output/
| output directory
Copy setuptools to output directory
cd /root/mx6q-python2.7/
cp setuptools-0.6c11-py2.7.egg output/sources/
The final directory format is as follows:
├── ntp-4.2.8.p10.tar.bz2
└── sources
├──openssl-OpenSSL_1_0_1b.tar.bz2
├── python2.7-for-arm.tar.bz2
└── setuptools-0.6c11-py2.7.egg
Now, the python compilation is complete. Please refer "python2.7 direct use manual" for the steps of porting to development board.
Process 2: Configure Python on development board
Copy the output folder generated above to iMX6Q development board
1. Unzip the package to development board
tar xvf python2.7-arm.tar.bz2 -C /
tar xvf openssl-OpenSSL_1_0_1b.tar.bz2 -C /
cp setuptools-0.6c11-py2.7.egg /
cp ntp-4.2.8p10-arm-no-share.tar.gz /
2. Modify the configuration
Modify /lib/pkgconfig/python2.pc as follows.
prefix=/
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Modify the first line of vi /bin/python2.7-config as follows.
#!/bin/python2.7
Add the following configuration to /etc/profile. After successful modification, it is source /etc/profile
export PYTHONHOME=/
export PYTHONPATH=$PYTHONHOME:$PYTHONHOME/lib/python2.7:$PYTHONHOME/lib:$PYTHONHOME/lib/python2.7/site-packages
export PATH=$PATH:$PYTHONHOME:$PYTHONPATH
3. Go to the folder easy_install and install easy_install.
Enter /bin/bash setuptools-0.6c11-py2.7.egg to install easy_install.
Modify vi /etc/profile,
Add alias easy_install='easy_install -i http://pypi.douban.com/simple'.
Save and exit to facilitate installation and management later, source /etc/profile to make the configuration effective
4. Cancel SSL global verification and verify easy_install, modify vi /bin/easy_install and add the following configuration.
import SSL
ssl._create_default_https_context = ssl._create_unverified_context
5. Configure network
Refer to other configuration application notes for methods of configuring network.
6. Installing ntp, enter ntp folder.
tar xvf ntp-4.2.8p10-arm-no-share.tar.gz -C /
ntpdate time.buptnet.edu.cn
Note: There may be errors here due to the ntp URL problem. You can directly search for the error message and solve it.
7. Install pip
easy_install pip After the installation is successful, pip -V to view the current installed version.
8. pip install module test
pip install requests
pip list to view the installed modules.
9. Import module test, enter python2.7 to enter python2.7 shell. Enter import requests, no error output. Type exit() to exit python shell.