在IPython中运行%%cythonmagic cell时,CompileError/LinkerError:“command'gcc'failed with exit status 1”是什么意思

2024-06-24 13:23:56 发布

您现在位置:Python中文网/ 问答频道 /正文

有时,当我在IPython笔记本上运行%%cython单元格时,我会得到一个很长的回溯,结尾是一个相当短的错误消息:

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

以及

^{4磅}$

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

下面是这样一个%%cython单元格的示例:

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

Tags: tree消息status错误withipython结尾exit
1条回答
网友
1楼 · 发布于 2024-06-24 13:23:56

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

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

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头,可以在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:

至少从Spyder3.3.3开始,编译器/链接器的输出可以在IPython控制台中看到(与独立的IPython控制台相同)。在


示例%%cython cell可以按如下方式进行修复:

^{pr2}$

相关问题 更多 >