编译.pyx文件时缺少numpy / arrayobject.h

时间:2013-09-27 18:12:55

标签: python macos numpy

来自enthought的安装天篷。在构建我的.pyx文件时,我收到此错误(后面跟着更多)

我是否需要easy_install其他软件包以获取“开发”版本,以便获取.h个文件?

gcc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -DNDEBUG -g -O3 -arch x86_64 -I/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/include/python2.7 -c tsBinner.c -o build/temp.macosx-10.6-x86_64-2.7/tsBinner.o
tsBinner.c:314:31: error: numpy/arrayobject.h: No such file or directory
tsBinner.c:315:31: error: numpy/ufuncobject.h: No such file or directory

更多上下文

这在几个Linux安装下编译并运行,但不适用于我最近安装的Canopy发行版python

这是setup.py

的内容
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("tsBinner",["tsBinner.pyx"])]

setup(
  name ='time stamp binner',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules 
)

以下是tsBinner.py的内容

from __future__ import division
import numpy as np
cimport numpy as np

#cimport cython
#@cython.boundscheck(False)
def tsBinner(np.ndarray[np.uint64_t, ndim=1] tstamps, \
    np.ndarray[np.int_t, ndim=1] bins):
    """
    bin the values of the tstamps array into the bins array.

    tstamps array is of type np.uint64

    bins array is of type int
    """
    cdef int nTStamps = tstamps.shape[0]
    cdef int iTStamp
    for iTStamp in range(nTStamps):
        bins[tstamps[iTStamp]] += 1
    return

以下是python和gcc

的版本
mac-119562:cosmic stoughto$ which python
/Users/stoughto/Library/Enthought/Canopy_64bit/User/bin/python
mac-119562:cosmic stoughto$ which gcc
/usr/bin/gcc
mac-119562:cosmic stoughto$ gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build     2336.11.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

mac-119562:cosmic stoughto$ python --version
Python 2.7.3 --  64-bit 

在MacBook Pro Mac OS X版本10.7.5上运行

3 个答案:

答案 0 :(得分:5)

这是一个相当古老的问题,但这是一个经常出现的问题,其他答案非常糟糕。硬连线numpy的include目录完全打破了虚拟环境,并且会使包很难自动分发和安装。解决此问题的正确方法是使用numpy.get_include()获取与当前加载的numpy版本关联的目录。 this对类似问题的回答显示了这一点。 setup.py就像这样:

import numpy
setup(
  name ='time stamp binner',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules,
  include_dirs = [numpy.get_include()] #Include directory not hard-wired
)

答案 1 :(得分:1)

您可以使用setup.py中的以下参数告诉编译器包含头文件所在的目录:

ext_modules = [Extension("tsBinner",["tsBinner.pyx"],
              include_dirs = ["/full/path/python2.7/site-packages/numpy/core/include/"])];

答案 2 :(得分:1)

这是所有Canopy发行版中都存在的问题。以下答案适用于Mac。在Linux和Windows中应该做的很明显:文件夹略有不同。


制作.c文件后,setup.py文件会自动为您运行以下内容

gcc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -DNDEBUG -g -O3 -arch x86_64 -I/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/include/python2.7 -c MyCythonCode.c -o build/temp.macosx-10.6-x86_64-2.7/MyCythonCode.o

然后您会收到有关丢失的.h文件的错误:arrayobject.hufuncobject.h

查看上面的代码,特别是-I选项,问题在于gcc正在寻找这些文件:

/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/include/python2.7

在终端窗口中键入以下内容,以查找这些文件的位置:

find /Applications/ -name "ufuncobject.h"

对于Mac上的Canopy安装,它们出现在几个地方。我感兴趣的位置是最新的Canopy更新文件夹(对我来说是1.1.0.1371)。所以这些文件在我这里找到了:

/Applications//Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/numpy/core/include/numpy/ufuncobject.h

请注意,错误消息要求在名为numpy的文件夹中包含两个文件。如果您只是复制有问题的两个文件,那么您将遇到更多错误。复制整个numpy文件夹,在这种情况下是位于(以前的一个文件夹)中的文件夹:

/Applications//Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/numpy/core/include/

将此文件夹粘贴到编译器正在查找的文件夹中(即上面列为-I选项的文件夹)。就我而言,这是:

/Applications//Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/

现在再次尝试运行安装文件。应该没问题。

我一直觉得这是Enthought发行版的一个问题。就像EPD和Canopy一样伟大,每次都必须手动修复它真的很痛苦。

希望这有帮助。