2014-07-02 98 views
14

我想用PyInstaller構建一個Python多文件代碼。爲此,我編譯了代碼Cython,並使用.so文件生成代替.py文件。使用PyInstaller構建Cython編譯的python代碼

假設第一個文件是main.py和進口的有file_a.pyfile_b.py,我得到file_a.so和用Cython編譯後file_b.so

當我把main.py,file_a.sofile_b.so放在一個文件夾中,並運行它"python main.py",它的工作原理。

但是,當我使用PyInstaller構建它並嘗試運行生成的可執行文件時,它將引發在file_afile_b中執行的導入錯誤。

這怎麼解決?一種解決方案是導入main.py中的所有標準模塊,並且這可行。但如果我不想更改我的代碼,那麼解決方案是什麼?

回答

12

所以我得到了這個爲你工作。

請看看Bundling Cython extensions with Pyinstaller

快速入門:

git clone https://github.com/prologic/pyinstaller-cython-bundling.git 
cd pyinstaller-cython-bundling 
./dist/build.sh 

這會產生一個靜態二進制:

$ du -h dist/hello 
4.2M dist/hello 
$ ldd dist/hello 
    not a dynamic executable 

,併產生輸出:

$ ./dist/hello 
Hello World! 
FooBar 

這基本上歸結爲製造簡單setup.py該構建擴展file_a.sofile_b.so,然後使用pyinstaller到擴展捆綁應用到單個executebla。

setup.py

from glob import glob 
from setuptools import setup 
from Cython.Build import cythonize 


setup(
    name="test", 
    scripts=glob("bin/*"), 
    ext_modules=cythonize("lib/*.pyx") 
) 

構建擴展:

$ python setup.py develop 

捆綁應用程序:

$ pyinstaller -r file_a.so,dll,file_a.so -r file_b.so,dll,file_b.so -F ./bin/hello 
+1

這完美的作品。感謝把它放在一起! – rth

+0

是的,沒有任何擔心!它很*很有趣! –

+0

有沒有與此等價的窗戶? ./dist/build.sh即使使用cygwin也不能識別 – Tetora