Cython: "치명적 오류: numpy/array 개체.h: 해당 파일 또는 디렉토리가 없습니다."
저는 여기서 싸이톤을 사용하여 답변 속도를 높이려고 합니다.나는 코드를 컴파일하려고 노력한 후에cygwinccompiler.py
hack은 여기에 설명되어 있습니다), 그러나 a를 얻습니다.fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated
오류입니다. 제 코드에 문제가 있는지, 아니면 사이튼에 대한 비밀스러운 미묘함인지 누가 말해줄 수 있나요?
아래는 제 코드입니다.
import numpy as np
import scipy as sp
cimport numpy as np
cimport cython
cdef inline np.ndarray[np.int, ndim=1] fbincount(np.ndarray[np.int_t, ndim=1] x):
cdef int m = np.amax(x)+1
cdef int n = x.size
cdef unsigned int i
cdef np.ndarray[np.int_t, ndim=1] c = np.zeros(m, dtype=np.int)
for i in xrange(n):
c[<unsigned int>x[i]] += 1
return c
cdef packed struct Point:
np.float64_t f0, f1
@cython.boundscheck(False)
def sparsemaker(np.ndarray[np.float_t, ndim=2] X not None,
np.ndarray[np.float_t, ndim=2] Y not None,
np.ndarray[np.float_t, ndim=2] Z not None):
cdef np.ndarray[np.float64_t, ndim=1] counts, factor
cdef np.ndarray[np.int_t, ndim=1] row, col, repeats
cdef np.ndarray[Point] indices
cdef int x_, y_
_, row = np.unique(X, return_inverse=True); x_ = _.size
_, col = np.unique(Y, return_inverse=True); y_ = _.size
indices = np.rec.fromarrays([row,col])
_, repeats = np.unique(indices, return_inverse=True)
counts = 1. / fbincount(repeats)
Z.flat *= counts.take(repeats)
return sp.sparse.csr_matrix((Z.flat,(row,col)), shape=(x_, y_)).toarray()
당신의setup.py
,그Extension
논쟁이 있어야 합니다.include_dirs=[numpy.get_include()]
.
또한, 당신은 실종되었습니다.np.import_array()
당신의 코드로.
--
설정 예.py:
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
setup(
ext_modules=[
Extension("my_module", ["my_module.c"],
include_dirs=[numpy.get_include()]),
],
)
# Or, if you use cythonize() to make the ext_modules list,
# include_dirs can be passed to setup()
setup(
ext_modules=cythonize("my_module.pyx"),
include_dirs=[numpy.get_include()]
)
귀사와 같은 단일 파일 프로젝트의 경우 다른 대안으로pyximport
생성할 필요가 없습니다.setup.py
IPython을 사용하는 경우 명령줄을 열 필요가 없습니다.모두 매우 편리합니다.이 경우 IPython 또는 일반 Python 스크립트에서 다음 명령을 실행해 보십시오.
import numpy
import pyximport
pyximport.install(setup_args={"script_args":["--compiler=mingw32"],
"include_dirs":numpy.get_include()},
reload_support=True)
import my_pyx_module
print my_pyx_module.some_function(...)
...
물론 컴파일러를 편집해야 할 수도 있습니다.이렇게 하면 가져오기 및 다시 로드 작업이 동일하게 수행됩니다..pyx
작업 중인 파일.py
파일
출처: http://wiki.cython.org/InstallingOnWindows
이 오류는 컴파일 중에 numpy 헤더 파일을 찾을 수 없음을 의미합니다.
해보세요export CFLAGS=-I/usr/lib/python2.7/site-packages/numpy/core/include/
컴파일을 수행합니다.이것은 몇 가지 다른 패키지의 문제입니다.같은 문제로 ArchLinux에 버그가 접수되었습니다. https://bugs.archlinux.org/task/22326
이 답변에 따라 설치한 경우numpy
와 함께pip
리눅스에서, 당신은 수동으로 심볼릭 링크를 설정해야 할 것입니다./usr/include/numpy
내 경우 경로는 다음과 같습니다.
sudo ln -s /usr/local/lib/python3.8/dist-packages/numpy/core/include/numpy/ /usr/include/numpy
설치 파일을 작성하고 포함 디렉터리의 경로를 파악하는 것이 귀찮다면 사이퍼를 사용해 보십시오.그것은 당신의 사이톤 코드를 컴파일하고 설정할 수 있습니다.include_dirs
자동으로 Numpy에 적용됩니다.
코드를 문자열에 로드한 다음 간단히 실행cymodule = cyper.inline(code_string)
그러면 당신의 기능은 다음과 같이 사용할 수 있습니다.cymodule.sparsemaker
순간적으로이런 거.
code = open(your_pyx_file).read()
cymodule = cyper.inline(code)
cymodule.sparsemaker(...)
# do what you want with your function
다음을 통해 사이퍼를 설치할 수 있습니다.pip install cyper
.
단순답변
더 간단한 방법은 파일에 경로를 추가하는 것입니다.distutils.cfg
Windows 7을 대신하는 경로는 기본적으로C:\Python27\Lib\distutils\
다음 내용만 주장하면 해결됩니다.
[build_ext]
include_dirs= C:\Python27\Lib\site-packages\numpy\core\include
전체 구성 파일
구성 파일의 모양을 예로 들자면, 전체 파일의 내용은 다음과 같습니다.
[build]
compiler = mingw32
[build_ext]
include_dirs= C:\Python27\Lib\site-packages\numpy\core\include
compiler = mingw32
그것은 그것을 할 수 있을 것입니다.cythonize()
여기서 언급한 것처럼 작동하지만 알려진 문제가 있기 때문에 작동하지 않습니다.
실행 중인 서버에 대한 권한이 없었고 CFLAGS 내보내기도 함께 작동하지 않았습니다.단순성을 위해 아나콘다(https://docs.anaconda.com/anaconda/install/) 를 설치하여 Numpy를 포함하여 설치된 모든 패키지에 대한 링크를 만듭니다.또한 너무 많은 공간을 사용하지 않도록 미니콘다를 설치하고 환경과 함께 작업할 수 있습니다.
언급URL : https://stackoverflow.com/questions/14657375/cython-fatal-error-numpy-arrayobject-h-no-such-file-or-directory
'programing' 카테고리의 다른 글
ggplot2의 주석에서 텍스트를 왼쪽 정렬하는 방법 (0) | 2023.07.01 |
---|---|
패키지에서 사용되지 않는 npm 패키지를 찾습니다.제이손 (0) | 2023.07.01 |
C# ASP.NET TLS를 통해 전자 메일 보내기 (0) | 2023.07.01 |
유형 스크립트로 프로젝트의 특정 디렉터리에서 가져오기 금지 (0) | 2023.06.26 |
루비민:Rubymin이 만든 .idea 파일을 Git가 무시하도록 만드는 방법. (0) | 2023.06.26 |