cython Bundling a Cython program using pyinstaller


Example

Start from a Cython program with a entrypoint:

def do_stuff():
    cdef int a,b,c
    a = 1
    b = 2
    c = 3
    print("Hello World!")
    print([a,b,c])
    input("Press Enter to continue.")

Create a setup.py file in the same folder:

from distutils.core import setup
from Cython.Build import cythonize
setup(
    name = "Hello World",
    ext_modules = cythonize('program.pyx'), 
)

Running it with python setup.py build_ext --inplace will produce a .pyd library in a subfolder.

After that, create a vanilla Python script using the library (e.g., main.py) and put the .pyd file beside it:

import program
program.do_stuff()

Use PyInstaller to bundle it pyinstaller --onefile "main.py". This will create a subfolder containing the executable of a 4 MB+ size containing the library plus the python runtime.