移植Python2.7到ARM-LINUX嵌入式平臺

xz -d Python-2.7.3.tar.xz
tar xvf Python-2.7.3.tar

編譯HOST版解釋器

編譯Python的嵌入式版需要解釋器解析setup.py從而編譯Python的模塊,因此需要先編譯出HOST的解釋器。

./configure
make python Parser/pgen
mv python hostpython
mv Parser/pgen Parser/hostpgen
make distclean

打交叉編譯補丁

點擊下載補丁

patch -p1 < Python-2.7.3-xcompile.patch

交叉編譯配置

設置交叉編譯工具鏈爲arm-none-linux-gnueabi,編譯生成的執行文件存放目錄爲當前目錄的_install文件夾。

./configure --host=arm-none-linux-gnueabi –prefix=KaTeX parse error: Expected 'EOF', got '&' at position 137: …px;"><strong>編譯&̲amp;安裝</strong>…PWD/_install

執行完以上命令之後在_install中產生bin lib include share 4個文件夾,避免麻煩可以將以上命令寫成一個腳本。

目標板文件拷貝

將_install/bin 中的所有內容拷貝到目標板的任意環境變量能夠訪問到的目錄即可,推薦/bin、/usr/bin。

將_install/lib 中的所有內容拷貝到目標板/lib中

將_install /include 中的所有內容拷貝到目標板/include中。因爲其中的某些頭文件是Python環境所需要的,比如Python解釋器啓動依賴於pyconfig.h,import time模塊時依賴timefuncs.h。

環境變量設置

將python2.7路徑加入到環境變量 PYTHONHOME、PYTHONPATH中。

如果是/etc/profile,在文件末尾添加如下信息然後執行 source /etc/profile。

export PYTHONPATH=/lib/python2.7:PYTHONPATH&lt;spanclass=&quot;hljskeyword&quot;&gt;export&lt;/span&gt;PYTHONHOME=&lt;spanclass=&quot;hljsregexp&quot;&gt;/lib/&lt;/span&gt;python2&lt;spanclass=&quot;hljsnumber&quot;&gt;.7&lt;/span&gt;:PYTHONPATH
&lt;span class=&quot;hljs-keyword&quot;&gt;export&lt;/span&gt; PYTHONHOME=&lt;span class=&quot;hljs-regexp&quot;&gt;/lib/&lt;/span&gt;python2&lt;span class=&quot;hljs-number&quot;&gt;.7&lt;/span&gt;:PYTHONHOME

如果是命令行,執行如下:

export PYTHONPATH=PYTHONPATH&lt;spanclass=&quot;hljssymbol&quot;&gt;:/lib/python2&lt;/span&gt;.&lt;spanclass=&quot;hljsnumber&quot;&gt;7&lt;/span&gt;exportPYTHONHOME=PYTHONPATH&lt;span class=&quot;hljs-symbol&quot;&gt;:/lib/python2&lt;/span&gt;.&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;
export PYTHONHOME=PYTHONHOME:/lib/python2.7

驗證目標板Python

打開解釋器,import 一些常用模塊,編寫程序測試。


後記

可能import time、datetime、threading、multiprocessing時提示缺少time、_collections、itertools等模塊。

這些是 builtin 模塊,可以通過sys.builtin_module_names查看。其源碼在Modules目錄中,編譯完成以 .so 的形式存放在/lib/python2.7/lib-dynload中。

首先在setup.py中 disabled_module_list=[] 是否將這些模塊disabled掉了。

如果依然無法解決,則在Modules/config.c中參考其他模塊的寫法添加代碼,手動編譯即可在build/lib.xx.2.7目錄中產生 .so 文件。


    



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章