尝试访问函数内部的numpy数组元素时出错Cython

时间:2018-07-16 21:55:50

标签: python cython cythonize

我正在尝试执行一个使用cython的简单python脚本。这是我的* .pyx脚本:

import numpy as np
cimport numpy as np

def cosmo_cythonize():
    cdef np.ndarray[double, mode="c", ndim=2] mat = np.zeros( [10 , 10], dtype=np.float64)

    print mat[5,0]   #Line1

    def func( ):
        a = 1
        #print mat[5,0]  #Line2

在此脚本中,当我尝试访问(打印)mat中矩阵Line1的任何元素时,整个脚本将正常运行。但是,当我尝试访问mat中的矩阵Line2的相同元素时,会出现以下错误。

running build_ext
cythoning cosmos_cy.pyx to cosmos_cy.c

Error compiling Cython file:
------------------------------------------------------------
...
import numpy as np
cimport numpy as np

def cosmo_cythonize():
    cdef np.ndarray[double, mode="c", ndim=2] mat = np.zeros( [10 , 10], dtype=np.float64)
                                      ^
------------------------------------------------------------

cosmos_cy.pyx:8:43: Buffer types only allowed as function local variables
building 'cosmos_cy' extension
gcc -fno-strict-aliasing -I/Users/Sidd/anaconda2/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Sidd/anaconda2/lib/python2.7/site-packages/numpy/core/include -I/Users/Sidd/anaconda2/include/python2.7 -c cosmos_cy.c -o build/temp.macosx-10.6-x86_64-2.7/cosmos_cy.o -O3
cosmos_cy.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
#error Do not use this file, it is the result of a failed Cython compilation.
 ^
error: command 'gcc' failed with exit status 1

我觉得这很奇怪。尝试访问函数mat中的矩阵func()的元素时,我在做什么错误?我将不胜感激任何帮助。提前致谢。

0 个答案:

没有答案