问题描述
我尝试对.py脚本进行cythonize.它是一个带有大量QToolButtons的PyQt5 gui,以及一个有效的EventFilter.c模块已成功构建,但是编译失败,并显示以下错误:
I try to cythonize a .py script. It is a PyQt5 gui with a large number of QToolButtons, and a working EventFilter. The c module is built successfully, however, the compilation fails with the following error:
编译器来自Visual Studio2019.Python3.5.5(是的,我知道是旧的,但是我有理由...).
The compiler comes from Visual Studio 2019. Python 3.5.5 (yes, old, I know, but I have reasons...).
在尝试"cythonize -i script.py"时,有什么方法可以增加堆空间??
Cython文档确实不清楚(至少对于非C专家而言……)
Cython documentation is really not clear on this (for a non-C-expert at least...)
编辑完整日志如下:
我只能补充说,该过程卡在了生成代码"列表中.发出消息约90秒,然后再引发C1002异常.
I can only add that the process gets stuck on the "Generating code" message for about 90 seconds before raising the C1002 exception.
设置文件相当标准:
from setuptools import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("MapForm2A.py")
)
该模块是纯PyQt5(pyuic5输出),没有其他依赖项,并且如果不使用Cython进行直接解释也可以正常工作.
The module is pure PyQt5 (pyuic5 output) with no other dependencies and works fine if interpreted directly with no Cython.
解决方案(也许有人会需要它.)感谢@DavidW(在下面的评论中讨论).
SOLUTION (Maybe someone will need it).Thanks to @DavidW (discussion in the comments below).
Setup.py必须通过以下方式进行修改:
Setup.py has to be modified in the following way:
from distutils import _msvccompiler
_msvccompiler.PLAT_TO_VCVARS['win-amd64'] = 'amd64'
from setuptools import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("MapForm2A.py"),
)
前两行强制使用64位工具链.
The first two lines force 64-bit toolchain.
推荐答案
出于对注释中已解决问题的更多解释的目的:基本问题似乎在于您正在编译大而复杂的MSVC链接步骤内存不足.
For the purposes of giving a little more explanation to something solved in the comments: the basic problem looks to be that you're compiling something large and complicated and MSVC has run out of memory at the linking step.
Microsoft有关于此错误的页面,其中包含许多选项,主要的选项是使用64位编译器.(请注意,这与您要编译的是32位还是64位模块无关-仅仅是编译器可执行文件的选择)
Microsoft has a page about this error which suggests a number of options, with the main one being to use a 64-bit compiler. (Note that this is independent of whether you're compiling a 32-bit or 64-bit module - it's merely the choice of compiler executable)
在编译Python扩展模块(尤其是setup.py)时,通常由distutils选择编译器设置.不幸的是,看起来distutils选择强制使用32位编译器(请参见 https://github.com/python/cpython/blob/e488e300f5c01289c10906c2e53a8e43d6de32d8/Lib/distutils/_msvccompiler.py#L160 ).
When compiling Python extension modules (especially with setup.py) the compiler setup is typically selected by distutils. Unfortunately, it looks like distutils chooses to force a 32-bit compiler (see https://github.com/python/cpython/blob/e488e300f5c01289c10906c2e53a8e43d6de32d8/Lib/distutils/_msvccompiler.py#L160).
我的建议是在setup.py顶部深入distutils内部(在进行任何实际设置之前)以覆盖此设置
My suggestion was to dig into the distutils internals at the top of setup.py (before any real setup takes place) to override this setting
from distutils import _msvccompiler
_msvccompiler.PLAT_TO_VCVARS['win-amd64'] = 'amd64'
基本上,您实际上要做的就是将选项 amd64
传递给microsoft提供的用于设置其编译器的 vcvarsall.bat
脚本,从而获得64位编译器来构建64位扩展.
Essentially all you're really doing is passing the option amd64
to the vcvarsall.bat
script that microsoft supply to set up their compiler, thus getting a 64-bit compiler to build a 64-bit extension.
这篇关于Cythonize以“致命错误C1002:编译器在第2遍中的堆空间不足"结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!