2017-06-21 1218 views
0

類似的問題已經被問了很多很多次,但不幸的是我再次遇到了使用Cython和Numpy的問題。拿這個小例子(這幾乎遵循例here):「cimport numpy」的Cython失敗

# file test.pyx 
import numpy as np 
#cimport numpy as np 

def func(): 
    print("hello") 

這我嘗試建立具有:

from distutils.core import setup 
from Cython.Build import cythonize 
import numpy as np 
import os 

os.environ["CC"] = "g++-7" 

setup(
    ext_modules = cythonize("test.pyx", include_path = [np.get_include()]) 
) 

這個例子工程(python setup.py build_ext --inplace),直到我取消註釋cimport ...線,在這之後,我得到了衆所周知的錯誤:

fatal error: numpy/arrayobject.h: No such file or directory

通過np.get_include()返回的路徑確實有arrayobject.h頭,但在被執行的實際g++命令,將包括目錄缺少爲-I/...

g++-7 -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c test.c -o build/temp.macosx-10.11-x86_64-3.6/test.o

任何想法可能會導致這個問題?

我在Mac OS上使用Python 3.6.1,使用pip3和Cython 0.25.2安裝了一切(Cython,Numpy,..)。

+0

你可以試試:'ext_modules = cythonize((擴展名( 「測試」, 來源= 「test.pyx」], include_dirs = [np.get_include()], ), ))' –

+0

還,cython的版本是什麼? 'cython3 --version' –

+0

皮埃爾,這是魔術!它有效,但我不知道爲什麼/如何(還) – Bart

回答

2

取而代之的是簡單cythonize命令,使用

ext_modules = cythonize((Extension("test", sources=["test.pyx"], include_dirs=[np.get_include()],),)) 

include_dirs選項這裏給出的使用include_path與「cythonize」,而不是「附加信息」。