Building Cython-compiled python code with PyInstaller

一笑奈何 提交于 2019-12-29 14:22:52

问题


I am trying to build a Python multi-file code with PyInstaller. For that I have compiled the code with Cython, and am using .so files generated in place of .py files.

Assuming the 1st file is main.py and the imported ones are file_a.py and file_b.py, I get file_a.so and file_b.so after Cython compilation.

When I put main.py, file_a.so and file_b.so in a folder and run it by "python main.py", it works.

But when I build it with PyInstaller and try to run the executable generated, it throws errors for imports done in file_a and file_b.

How can this be fixed? One solution is to import all standard modules in main.py and this works. But if I do not wish to change my code, what can be the solution?


回答1:


So I got this to work for you.

Please have a look at Bundling Cython extensions with Pyinstaller

Quick Start:

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

This produces a static binary:

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

And produces the output:

$ ./dist/hello 
Hello World!
FooBar

Basically this came down to producing a simple setup.py that builds the extensions file_a.so and file_b.so and then uses pyinstaller to bundle the application the extensions into a single executebla.

Example 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")
)

Building the extensions:

$ python setup.py develop

Bundling the application:

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


来源:https://stackoverflow.com/questions/24525861/building-cython-compiled-python-code-with-pyinstaller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!