python黏合flash"/>
python黏合flash
背景
想在嵌入式linux系统上使用python3.
嵌入式平台:飞思卡尔(NXP)的IMX280 ARM7
python版本:3.7
host虚拟机:ubantu12.4 32位
编译
python官网下载源码包
编译host虚拟机中运行的python
因为等一下交叉编译的时候要用到。编译比较简单
./configure --prefix=/home/bert/python3.7-x86
make
make install
ln -s /home/bert/python3.7-x86/bin/python3.7 /usr/bin/python3
报错:No module named '_ctypes'
解决:sudo apt-get install libffi.dev
编译arm版本的python
编译arm版本比较麻烦,写一个config脚本,名为bertconfig-arm.sh。
#!/bin/sharm_build=`pwd`/arm_buildmkdir$arm_build
cd $arm_buildecho ac_cv_file__dev_ptmx=yes >config.siteecho ac_cv_file__dev_ptc=yes >>config.site
export CONFIG_SITE=config.site
../configure \
CC=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-gcc\
CXX=CC=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-g++\
AR=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-ar\
READELF=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-readelf \
STRIP=/opt/freescale/usr/local/gcc-4.1.2-glibc-2.5-nptl-3/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-strip \--host=arm-none-linux-gnueabi \--build=i686-linux-gnu \--target=arm-none-linux-gnueabi \--disable-ipv6 \--prefix=/home/bert/python3.7-arm
exit0
make clean
./bertconfig-arm.sh
make
make install
瘦身
编译通过后arm版python160M+,我的嵌入式产品flash才150M空间,没法用,要瘦身。
删除不必要的文件
删除include和share文件夹
bin和lib下面仅留下python3.7
/lib/python3.7下删除所有test和config相关的文件
strip缩小体积
将 bin下的python3.7 和lib/python3.7下的所有.so strip,基本可以减小一半的体积
arm-strip /bin/python3.7
arm-strip /lib/python3.7/lib-dynload/*.so
将.py转换成.pyc文件
其实__pycache__里面放的是.py自动生成的.pyc文件,现在手动转换后,就可以减小一半的空间。
python3 -m compileall . -b
删除__pycatche__和所有.py
经过以上步骤,体积缩小到50M左右勉强可以用啦。
其他思路:
创建zip版本的lib:可以是可以,但是运行的时候还是要解压呀,增加第三方库的时候也不方便吧?
使用其他压缩软件:比较麻烦吧,不知道会不会影响性能。
终极瘦身之最小python
lib下面有好多包,并非都是必须的,例如在没有显示屏的arm中,要tkinter作甚?
lib/python3下面保留lib-dynload(动态库)和os.pyc这两个文件
在arm系统上运行bin/python3.7,提示差什么包就把什么包复制过去
最终lib结构如下:
-rw-r--r-- 1 bert bert 29K Oct 11 02:37 _collections_abc.pyc
-rw-r--r-- 1 bert bert 3.4K Oct 11 02:37 _sitebuiltins.pyc
-rw-r--r-- 1 bert bert 6.3K Oct 11 02:37 abc.pyc
-rw-r--r-- 1 bert bert 34K Oct 11 02:37 codecs.pyc
drwxr-xr-x 3 bert bert 12K Oct 11 04:12 encodings
-rw-r--r-- 1 bert bert 3.7K Oct 11 02:37 genericpath.pyc
-rw-r--r-- 1 bert bert 3.3K Oct 11 02:37 io.pyc
drwxr-xr-x 2 bert bert 4.0K Oct 12 2019 lib-dynload
-rw-r--r-- 1 bert bert 29K Oct 11 02:37 os.pyc
-rw-r--r-- 1 bert bert 11K Oct 11 02:37 posixpath.pyc
-rw-r--r-- 1 bert bert 17K Oct 11 02:37 site.pyc
-rw-r--r-- 1 bert bert 3.8K Oct 11 02:37 stat.pyc
现在只有13M了,完全满足我的要求。
encodings文件夹下面肯定可以继续缩减,不过已经超出我的需求了,到此为止。
参考
好多一键移植的方法,我个人觉得,在linux里面,No way.
不得不说的两篇好文章供参考:
更多推荐
python黏合flash
发布评论