【问题标题】:cx_Freeze fails to include Cython .pyx modulecx_Freeze 无法包含 Cython .pyx 模块
【发布时间】:2015-07-30 21:18:36
【问题描述】:

我有一个 Python 应用程序,我最近添加了一个 Cython 模块。使用 pyximport 从脚本运行它可以正常工作,但我还需要一个使用 cx_Freeze 构建的可执行版本。

麻烦的是,尝试构建它会给我一个可执行文件,该可执行文件会引发 ImportError 尝试导入 .pyx 模块。

我像这样修改了我的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 中。

我该如何进行这项工作?

【问题讨论】:

    标签: python python-3.x cython cx-freeze


    【解决方案1】:

    解决方案是对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']
          )
    

    【讨论】:

    • 仅供参考,为了使其正常工作,您必须输入“python setup-cython.py build_ext --inplace”,然后输入“python setup-cython.py build”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多