cx_Freeze 未能包含 Cython .pyx 模块

cx_Freeze fails to include Cython .pyx module

我有一个 Python 应用程序,我最近在其中添加了一个 Cython 模块。 运行 它来自带有 pyximport 的脚本工作正常,但我还需要一个我用 cx_Freeze.

构建的可执行版本

问题是,尝试构建它给我一个可执行文件,该可执行文件在尝试导入 .pyx 模块时引发 ImportError。

我像这样修改了我的 setup.py 以查看是否可以先编译 .pyx 以便 cx_Freeze 可以成功打包它:

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

...但是在构建时给我的只是 No module named fx 在 cx_Freeze 内。

我该如何进行这项工作?

解决方案是对 setup() 进行两次单独调用;一个用 Cython 构建 fx.pyx,然后一个用 cx_Freeze 打包 exe。这是修改后的 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']
      )