繁体   English   中英

在 IPython 中运行 %%cython-magic 单元时,CompileError/LinkerError: "command 'gcc' failed with exit status 1" 是什么意思

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

有时,当我在 IPython 笔记本中运行 %%cython-cell 时,我会得到一个很长的回溯,并以非常短的错误消息结束:

CompileError:命令“gcc”失败,退出状态为 1

或者

LinkError:命令“gcc”失败,退出状态为 1

在 Windows 上,相应的消息是:

CompileError: 命令 'C:.\\Microsoft Visual Studio\\..\\cl.exe' 失败,退出状态为 X

LinkError: 命令 'C:..\\Microsoft Visual Studio\\..\\link.exe' 失败,退出状态为 YYYY

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

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

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

%%cython魔法使用distutils在幕后构建 Cython 扩展,IPython不会捕获输出 gcc 或其他编译器/链接器日志到标准错误/输出。

为了查看编译器/链接器记录的错误和警告,必须转到编译器记录错误的位置,这取决于 IPython 的启动方式。

在 Linux 上还有另一种可能性:安装wurlitzer包并通过%load_ext wurlitzer激活它,这也将捕获 gcc 的输出并将其显示在笔记本中,另请参阅此答案

遗憾的是, wurlitzer仅适用于 Linux,而以下选项适用于任何操作系统。

IPython/Jupiter 笔记本:

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

/home/ed/.cache/ipython/cython/_cython_magic_5f6d267a6f541c572b4517a74d5c9aad.c:607:31:致命错误numpy/arrayobject.h :没有这样的文件或目录编译终止。

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

IPython 控制台

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

 >>> 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 3.3.3 开始,编译器/链接器的输出可以在 IPython 控制台中看到(与在独立的 IPython 控制台中相同)。


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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM