cython Cython bundling Bundling a Cython program using pyinstaller

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any cython Question?