【问题标题】:Cython: fatal error: 'numpy/arrayobject.h' file not found, using numpyCython:致命错误:使用 numpy 找不到“numpy/arrayobject.h”文件
【发布时间】:2016-12-31 01:08:01
【问题描述】:

我正在尝试将我的 Ipython 笔记本代码移动到 python。但我有错误

fatal error: 'numpy/arrayobject.h' file not found
#include "numpy/arrayobject.h"

,即使我在设置中包含了 numpy

我的 setup.py:

from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=cythonize("Trajectory.pyx"),
    include_dirs=[numpy.get_include()]
)

Trajectory.pyx 文件

cimport numpy as np
import  numpy as np 

我在 osX,Python 2.7.10 上运行

它还在错误之前为我提供了此信息,希望这有助于识别问题: clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -I/Applications/Xcode.app/ Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -我/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/2.7.10_2/Frameworks/Python。 framework/Versions/2.7/include/python2.7 -c Trajectory.c -o build/temp.macosx-10.11-x86_64-2.7/Trajectory.o

当我跑的时候

import numpy
numpy.get_include()

我明白了:

'/usr/local/lib/python2.7/site-packages/numpy/core/include'

然后我查看目录,/numpy/arrayobject.h 就在那里。所以我真的不知道为什么它说没有这样的文件

【问题讨论】:

  • documentation 建议您应该将include_path 传递给cythonizeinclude_dirs 传递给Extension 类。您似乎将include_dirs 传递给setup,这可能无法识别。
  • 嗨,谢谢。现在我让它以一种非常奇怪的方式工作。我首先执行 ext_modules=cythonize("Trajectory.pyx") 生成 Trajectory.c,但出现上述相同的错误。然后,使用 ext_modules=[ Extension("Trajectory", ["Trajectory.c"], include_dirs=[numpy.get_include()]),] 重新编译。生成.so.....知道如何纠正这个吗?
  • 我认为你应该要么做ext_modules = cythonize("Trajectory.pyx",include_path=[np...])要么做ext_modules=cythonize([Extension(...,include_dirs=[np...])])
  • 是的。这就是我所做的......但我必须先使用 cythonize 生成 .c 文件,然后使用扩展名(并注释掉 cythinize 位)以获得最终的 .so。如果我只使用它们中的任何一个,我都会遇到同样的错误。
  • 由于某种原因,include_dirs 和 include_parth 都无法正确添加。我最后使用 os.environ 解决了这个问题: os.environ["CPPFLAGS"] = os.getenv("CPPFLAGS", "") + "-I" + numpy.get_include()

标签: python numpy cython cythonize


【解决方案1】:

FWIW,我在 macOS(Python 3.7.6、Cython 0.29.14、macOS 10.15)上遇到了同样的问题 ('numpy/arrayobject.h' file not found)。

这是我用来获得正确包含路径的解决方法:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy

setup(name='Foo',
      ext_modules=cythonize([Extension("bar", ["bar.pyx"], include_dirs=[numpy.get_include()])])
)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-12-14
  • 2013-01-17
  • 2021-02-14
  • 2017-12-06
  • 2021-05-28
  • 1970-01-01
  • 2016-11-24
相关资源
最近更新 更多