运行 Jupyter 中的 Cython iPython

Running Cython in Jupyter iPython

运行 计时赛的几何级数迭代循环,使用 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

我能够使用 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

代码看起来不错。一定是你的设置有问题。

我知道这个问题很老,但我认为这可能对其他人有所帮助。

我 运行 在 Windows 上为一个旧的 Py2.7 项目解决了这个问题。

如果在 Windows 上,并使用 Py2.7 检查是否安装了适用于 Python 的 MS Visual Studio C++ 编译器 (download link)。不确定 Py3 需要进行哪些更改。

对于您的 anaconda 环境,找到 Lib\distutils 目录并创建一个 distutils.cfg 文件(如果不存在,否则只需根据需要修改当前文件)。

您希望构建配置如下所示。

[建造]<br> 编译器=msvc

如果在 linux,请确保您有必要的 devel 包可用,例如

Ubuntu: apt-get install python-devel