Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cx_Freeze fails to include Cython .pyx module

I have a Python application to which I recently added a Cython module. Running it from script with pyximport works fine, but I also need an executable version which I build with cx_Freeze.

Trouble is, trying to build it gives me an executable that raises ImportError trying to import the .pyx module.

I modified my setup.py like so to see if I could get it to compile the .pyx first so that cx_Freeze could successfully pack it:

from cx_Freeze import setup, Executable
from Cython.Build import cythonize


setup(name='projectname',
      version='0.0',
      description=' ',
      options={"build_exe": {"packages":["pygame","fx"]},'build_ext': {'compiler': 'mingw32'}},
      ext_modules=cythonize("fx.pyx"),
      executables=[Executable('main.py',targetName="myproject.exe",base = "Win32GUI")],
      requires=['pygcurse','pyperclip','rsa','dill','numpy']
      )

... but then all that gives me is No module named fx within cx_Freeze at build-time instead.

How do I make this work?

like image 582
Schilcote Avatar asked Sep 28 '22 04:09

Schilcote


1 Answers

The solution was to have two separate calls to setup(); one to build fx.pyx with Cython, then one to pack the exe with cx_Freeze. Here's the modified setup.py:

from cx_Freeze import Executable
from cx_Freeze import setup as cx_setup
from distutils.core import setup
from Cython.Build import cythonize

setup(options={'build_ext': {'compiler': 'mingw32'}},
      ext_modules=cythonize("fx.pyx"))

cx_setup(name='myproject',
      version='0.0',
      description='',
      options={"build_exe": {"packages":["pygame","fx"]}},
      executables=[Executable('main.py',targetName="myproject.exe",base = "Win32GUI")],
      requires=['pygcurse','pyperclip','rsa','dill','numpy']
      )
like image 170
Schilcote Avatar answered Sep 29 '22 18:09

Schilcote