cx_Freeze fails to include Cython .pyx module

孤者浪人 提交于 2019-12-21 19:21:16

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/31734230/cx-freeze-fails-to-include-cython-pyx-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!