python-cx_freeze无法包含Cython .pyx模块
作者:互联网
我有一个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']
)
…但是,所有给我的就是在构建时在cx_freeze中没有名为fx的模块.
我该如何工作?
解决方法:
解决的办法是对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-3-x,cython,cx-freeze,python 来源: https://codeday.me/bug/20191119/2040054.html