Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Cython-compiled python code with PyInstaller

Tags:

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?

like image 213
Rajat Avatar asked Jul 02 '14 07:07

Rajat


People also ask

Can Cython compile all Python code?

You can't, Cython is not made to compile Python nor turn it into an executable.

Does Cython compile to exe?

To compile the Cython source code to a C file that can then be compiled to an executable you use a command like cython myfile. pyx --embed and then compile with whichever C compiler you are using.


2 Answers

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 executeble.

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 
like image 128
James Mills Avatar answered Sep 23 '22 17:09

James Mills


Just in case someone's looking for a quick fix.

I ran into the same situation and found a quick/dirty way to do the job. The issue is that pyinstaller is not adding the necessary libraries in the .exe file that are needed to run your program.

All you need to do is import all the libraries (and the .so files) needed into your main.py file (the file which calls file_a.py and file_b.py). For example, assume that file_a.py uses opencv library (cv2) and file_b.py uses matplotlib library. Now in your main.py file you need to import cv2 and matplotlib as well. Basically, whatever you import in file_a.py and file_b.py, you have to import that in main.py as well. This tells pyinstaller that the program needed these libraries and it includes those libraries in the exe file.

like image 31
Chris Henry Avatar answered Sep 21 '22 17:09

Chris Henry