使用PyInstaller构建Cython编译的python代码

问题描述:

我想用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中的所有标准模块,并且这可行。但如果我不想更改我的代码,那么解决方案是什么?

所以我得到了这个为你工作。

请看看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