Skip to content
Advertisement

How to compile python script importing modules to executable using cython

Maybe this is a duplicate, but I couldn’t find an answer for this particular situation: I want to compile and distribute my python Code to c and then to a binary using gcc. For distribution, I use appimagetool to include libraries.

I use the command

cython3 -3 myapp.py --embed

to create the C code, and then

gcc myapp.c -o myapp $(pkg-config --libs --cflags python3)

to generate the executable. This works, but when I transfer the executable to another machine, I get the error

No Module named AnyRandomModule

Where AnyRandomModule is a python module I am importing. When I install it via

pip3 install AnyRandomModule

it works. But I don’t want that the user needs to install the libs himself. How can I bundle my imported python modules in my executable, or otherwise compile them to .so shared library files from which I can import? I already tried to convert them to .so files using the quick start example from the Cython documentation, but I still get the same error when executing the myapp executable, even though those .so files are in the same directory.

Advertisement

Answer

you can use this answer: Building Cython-compiled python code with PyInstaller

to bundle everything together (use cython and then pack everything using pyinstaller which should pack all the packages)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement