cx_freeze fails to include Cython .pyx module
Question: 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?


Answer: 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']
          )
    

