What does CompileError/LinkerError: "command 'gcc' failed with exit status 1" mean, when running %%cython-magic cell in IPython

What does CompileError/LinkerError: "command 'gcc' failed with exit status 1" mean, when running %%cython-magic cell in IPython

有时,当我 运行 一个 IPython-notebook 中的 %%cython-cell 时,我得到一个很长的回溯,以一个非常短的错误消息结尾:

CompileError: command 'gcc' failed with exit status 1

LinkError: command 'gcc' failed with exit status 1

在Windows上对应的消息是:

CompileError: command 'C:.\Microsoft Visual Studio\..\cl.exe' failed with exit status X

LinkError: command 'C:..\Microsoft Visual Studio\..\link.exe' failed with exit status YYYY

是否可以获得有关潜在错误的更准确信息?

这种 %%cython-cell 的示例如下:

[1] %load_ext Cython
[2] %%cython
    from sklearn.tree._tree cimport Node
    print("loaded")

%%cython 魔法使用 distutils 在引擎盖下构建 Cython 扩展并且 IPython 不会 捕获输出gcc 或其他 compilers/linkers 日志到标准 error/output.

为了查看 compiler/linker 记录的错误和警告,必须转到编译器记录错误的地方,这取决于 IPython 的方式开始了。

在 Linux 上还有另一种可能性:安装 wurlitzer package and activate it via %load_ext wurlitzer, which would also capture the output from gcc and show it in the notebook, see also

遗憾的是,wurlitzer 仅适用于 Linux,而下面的选项适用于任何 OS。

IPython/Jupiter笔记本:

当笔记本从终端启动时,例如通过 ipython notebook 或类似的方式,然后可以在此终端中看到编译器输出 - 我们看到上述单元格的问题是:

/home/ed/.cache/ipython/cython/_cython_magic_5f6d267a6f541c572b4517a74d5c9aad.c:607:31: fatal error: numpy/arrayobject.h: No such file or directory compilation terminated.

缺少 numpy-headers,可以在 numpy.get_include().

中找到

IPython-控制台

如果 IPython 从终端启动,错误将直接记录到 IPython 控制台。但请注意:compiler/linker 输出将直接出现在错误跟踪的开头:

 >>> ipython
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.                                                                       

In [1]: %load_ext Cython                                                        

In [2]: %%cython 
   ...: from sklearn.tree._tree cimport Node 
   ...: print("loaded") 
   ...:  
   ...: 
/home/ed/.cache/ipython/cython/_cython_magic_1182f410e5c0a56b03b28dd88700704d.c:607:31: fatal error: numpy/arrayobject.h: No such file or directory
compilation terminated.
---------------------------------------------------------------------------
DistutilsExecError                        Traceback (most recent call last)
....

CompileError: command 'gcc' failed with exit status 1

第一行告诉我们我们需要知道的一切!

Spyder:

至少从 Spyder 3.3.3 开始,compiler/linker 的输出可以在 IPython 控制台中看到(与独立的 IPython 控制台相同)。


示例 %%cython-cell 可以修复如下:

%%cython -I <path from numpy.get_include()>
from sklearn.tree._tree cimport Node
print("loaded")