cx_Freeze 无法包含 Cython .pyx 模块

Sch*_*ote 5 python cython cx-freeze python-3.x

我有一个 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']
      )
Run Code Online (Sandbox Code Playgroud)

...但是所有给我的都是No module named fx在构建时的 cx_Freeze 中。

我如何使这项工作?

Sch*_*ote 5

解决方案是两次单独调用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']
      )
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,为了使其正常工作,您必须输入“python setup-cython.py build_ext --inplace”,然后输入“python setup-cython.py build” (3认同)