在Jupyter iPython中运行Cython

12

使用Cython接口在时间试验中运行几何级数的迭代循环。

在编译时出现错误(shift-enter):CompileError:命令“gcc”以1的退出状态失败

%load_ext Cython

%%cython
def geo_prog_cython(double alpha, int n):
    cdef double current = 1.0
    cdef double sum = current
    cdef int i
    for i in range(n):
        current = current * alpha
        sum = sum + current
    return sum

错误:

//anaconda/lib/python3.5/distutils/command/build_ext.py in build_extension(self, ext)
    530                                          debug=self.debug,
    531                                          extra_postargs=extra_args,
--> 532                                          depends=ext.depends)
    533 
    534         # XXX outdated variable, kept here in case third-part code

没有看到任何错误。请粘贴完整的错误报告。 - hruske
DistutilsExecError
//anaconda/lib/python3.5/distutils/spawn.py in _spawn_posix(cmd, search_path, verbose, dry_run) 158 "命令 %r 执行失败,退出状态码为 %d" --> 159 % (cmd, exit_status)) 160 elif os.WIFSTOPPED(status):DistutilsExecError: 命令 'gcc' 执行失败,退出状态码为 1
- rrg
2
请您能否粘贴完整的回溯信息,而不仅仅是错误信息。 - hruske
你尝试过在Jupyter以外的环境中使用Cython吗?我认为这可能是你的Cython配置出了问题。 - Tadhg McDonald-Jensen
1
有什么提示可以告诉我们什么是正确的Cython设置吗? - rrg
2个回答

6

我知道这个问题很旧,但我认为这可能对某些人有所帮助。

我在使用旧版 Py2.7 项目时遇到了这个问题,而且是在 Windows 上。

如果你正在 Windows 上使用 Py2.7,请检查你是否安装了适用于 Python 的 MS Visual Studio C++ 编译器 (下载链接)。不确定 Py3 需要做哪些更改。

对于你的 Anaconda 环境,请找到 Lib\distutils 目录并创建一个 distutils.cfg 文件(如果还没有,请根据需要修改当前文件)。

你需要让构建配置看起来像下面这样:

[build]
compiler=msvc

如果在 Linux 上,请确保你有必要的 devel 包可用,例如:

Ubuntu: apt-get install python-devel


6

我使用Anaconda3能够在没有错误的情况下重现此问题:

%load_ext Cython
%%cython -a

def geo_prog_cython(double alpha, int n):
    cdef double current = 1.0
    cdef double sum = current
    cdef int i
    for i in range(n):
        current = current * alpha
        sum = sum + current
    return sum

例子:

geo_prog_cython(0.5, 5)
1.96875

代码看起来没问题。可能是你的设置出了问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接