本文介绍了cx_Freeze无法包含Cython .pyx模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

Trouble is, trying to build it gives me an executable that raises ImportError trying to import the .pyx module.

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

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

...但是全部给我的是在构建时,cx_Freeze中没有名为fx 的模块。

... but then all that gives me is No module named fx within cx_Freeze at build-time instead.

我该如何工作?

推荐答案

解决方案是对 setup()进行两个单独的调用;一个使用Cython构建 fx.pyx ,然后另一个使用cx_Freeze打包exe。以下是修改后的 setup.py

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

这篇关于cx_Freeze无法包含Cython .pyx模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:07