问题背景

Cython是用来加速Python程序性能的一个工具,其基本使用逻辑就是将类Python代码(*.pyx扩展格式)编译成\(*.c,*.so\)动态链接库文件,然后就可以在正常的Python脚本文件中调用动态链接库的内部函数。编译过程中因为会去索引一些头文件,如果找不到路径就有可能报错。

测试案例

我们可以用Cython做一个简单的基于numpy array输入的求和函数:

# test_sum.pyx
import numpy as np
cimport numpy as np cpdef double my_sum(double[:] arr):
cdef double s = 0.0
cdef int i
for i in range(arr.shape[0]):
s += arr[i]
return s def main():
a = np.array([1.0, 2.0, 3.0])
print(my_sum(a))

然后编译:

$ cythonize -i test_sum.pyx
running build_ext
building 'test_sum' extension
creating /home/mindsponge/tests/tmpwo3gq_ad/home
creating /home/mindsponge/tests/tmpwo3gq_ad/home/mindsponge
creating /home/mindsponge/tests/tmpwo3gq_ad/home/mindsponge/tests
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/python-3.7.5/include/python3.7m -c /home/mindsponge/tests/test_sum.c -o /home/mindsponge/tests/tmpwo3gq_ad/home/mindsponge/tests/test_sum.o
/home/mindsponge/tests/test_sum.c:1240:10: fatal error: numpy/arrayobject.h: No such file or directory
#include "numpy/arrayobject.h"
^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'gcc' failed with exit status 1

这个报错是找不到numpy下的一个头文件,那么解决问题的思路就很直接,找到相关头文件的路径,添加到gcc编译的环境变量中即可。先看一下numpy的安装路径:

$ python3 -m pip show numpy
Name: numpy
Version: 1.21.6
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email:
License: BSD
Location: /usr/local/python-3.7.5/lib/python3.7/site-packages
Requires:
Required-by: bitshuffle, fabio, h5py, matplotlib, mindinsight, mindspore, mindspore-serving, pandas, pyopencl, scikit-learn, scipy, silx, Xponge

然后在这个Location中找到对应的头文件:

$ find /usr/local/python-3.7.5/lib/python3.7/site-packages -name 'arrayobject.h'
/usr/local/python-3.7.5/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h

找到以后先查看一下环境变量中是否已有值,然后再将上面这个路径添加到环境变量中:

$ export | grep C_INCLUDE_PATH
$ export C_INCLUDE_PATH=/usr/local/python-3.7.5/lib/python3.7/site-packages/numpy/core/include/

再次执行编译:

# cythonize -i test_sum.pyx
running build_ext
building 'test_sum' extension
creating /home/mindsponge/tests/tmpw_icoc3b/home
creating /home/mindsponge/tests/tmpw_icoc3b/home/mindsponge
creating /home/mindsponge/tests/tmpw_icoc3b/home/mindsponge/tests
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/python-3.7.5/include/python3.7m -c /home/mindsponge/tests/test_sum.c -o /home/mindsponge/tests/tmpw_icoc3b/home/mindsponge/tests/test_sum.o
In file included from /usr/local/python-3.7.5/lib/python3.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1969:0,
from /usr/local/python-3.7.5/lib/python3.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
from /usr/local/python-3.7.5/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
from /home/mindsponge/tests/test_sum.c:1240:
/usr/local/python-3.7.5/lib/python3.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it with " \
^~~~~~~
gcc -pthread -shared /home/mindsponge/tests/tmpw_icoc3b/home/mindsponge/tests/test_sum.o -L/usr/local/python-3.7.5/lib -lpython3.7m -o /home/mindsponge/tests/test_sum.cpython-37m-x86_64-linux-gnu.so

编译顺利通过,并且会在当前路径下生成一个*.c文件和一个*.so动态链接库文件。然后就可以在python中直接引用动态链接库的内部函数:

In [1]: import numpy as np

In [2]: a = np.array([1.0, 2.0, 3.0])

In [3]: from test_erf import my_sum

In [4]: my_sum(a)
Out[4]: 6.0

总结概要

本文介绍了一个在使用Cython进行Python高性能编程时有可能遇到的一个问题,就是找不到的对应的C语言的头文件,例如numpy中的一些头文件。解决思路就是先在本地找到相应的头文件路径,然后将其添加到编译器的环境变量中即可。

版权声明

本文首发链接为:https://www.cnblogs.com/dechinphy/p/win11-vmd.html

作者ID:DechinPhy

更多原著文章:https://www.cnblogs.com/dechinphy/

请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

Cython编译报错“numpy/arrayobject.h: No such file or directory”解决方案的更多相关文章

  1. Linux安装redis报错:jemalloc/jemalloc.h: No such file or directory踩坑

    报错内容: 针对这个错误,我们可以在README.md 文件中看到解释: --------- Selecting a non-default memory allocator when buildin ...

  2. redis 安装报错 jemalloc/jemalloc.h: No such file or directory。

    对于redis安装的这个错误,我在博客redis 安装 与错误解决办法最后有提及,但是网上大部分文章的对这个问题的解答都是有误的.所以在这里单列出来. 错误内容: jemalloc/jemalloc. ...

  3. 「caffe编译bug」python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory

    在Makefile.config找到PYTHON_INCLUDE,发现有点不同: PYTHON_INCLUDE := /usr/include/python2.7 \         /usr/lib ...

  4. 配置caffe的python环境时make pycaffe提示fatal error: numpy/arrayobject.h No such file or directory解决方法

    重装numpy: sudo pip uninstall numpy sudo pip install numpy 是没有用的... 解决的办法就是: sudo apt-get install pyth ...

  5. numpy/arrayobject.h”: No such file or directory

    import numpyimport pyximportpyximport.install(setup_args={"script_args":["--compiler= ...

  6. pip install urllib3[secure] 报错 error: ffi.h: No such file or directory

    解决 sudo apt-get install build-essential autoconf libtool pkg-config python-opengl python-imaging pyt ...

  7. 错误 1 error C1083: 无法打开包括文件: “numpy/arrayobject.h”: No such file

    问题:错误 1 error C1083: 无法打开包括文件: “numpy/arrayobject.h”: No such file 解答:加入include路径:E:\env\Anaconda2x6 ...

  8. python能够执行,但编译第三包遇到 python.h no such file or directory

    python能够执行,但编译第三包遇到 python.h no such file or directory 这个问题是由于没有安装python-devel, 安装此包就能够解决次问题,在Linux下 ...

  9. node 报错 env: node\r: No such file or directory

    最近在编写一个命令行工具.使用 npm link 时可以正常运行.但是 ctrl+s 保存后, 再运行则报错 env: node\r: No such file or directory ,需要再 n ...

  10. 使用nsenter进入docker容器后端报错 mesg: ttyname failed: No such file or directory

    通过nsenter 进入到docker容器的后端总是报下面的错,, [root@devdtt ~]# docker inspect -f {{.State.Pid}} mynginx411950 [r ...

随机推荐

  1. 2018-8-10-WPF-省市县3级联动

    title author date CreateTime categories WPF 省市县3级联动 lindexi 2018-08-10 19:16:53 +0800 2018-2-13 17:2 ...

  2. vue+vant+js实现购物车原理小demo(基础版)

    电商毕业设计里的一个购物车demo,拿vue+vant需要写的核心计算代码只有12行.效果图: main.js: Vue.use(Stepper); .vue文件 <template> & ...

  3. 四、【转】基于知识图谱的推荐系统(KGRS)综述

    以下文章来源于AI自然语言处理与知识图谱 ,作者Elesdspline 导语 本文是2020年针对知识图谱作为辅助信息用于推荐系统的一篇综述.知识图谱对于推荐系统不仅能够进行更精确的个性化推荐,而且对 ...

  4. GOLANG-配置nginx反向代理端口 配置域名

    目录 配置/etc/nginx/nginx.conf文件 新建/etc/nginx/conf.d/doc.haimait.conf文件 重启nginx服务 解析自己的域名到服务器的公网ip 配置/et ...

  5. WebKist Inside: CSS 样式表的组成

    1 StyleSheet 一张 StyleSheet 由一系列 Rules 组成,这些 Rules 可以分成 2 大类: 1 Style Rule 2 At-Rule 下面的例子展示了 Style R ...

  6. ECMAScript 语言规范每年都会进行一次更新,而备受期待的 ECMAScript 2024 将于 2024 年 6 月正式亮相。目前,ECMAScript 2024 的候选版本已经发布,为我们带来了一系列实用的新功能。

    Promise.withResolvers 使用 Promise.withResolvers() 关键的区别在于解决和拒绝函数现在与 Promise 本身处于同一作用域,而不是在执行器中被创建和一次性 ...

  7. JDK源码阅读-------自学笔记(九)(常用类型Integer初探)

    常用类 主要分为几部分需要学习: 包装类的介绍和使用 字符串的介绍和使用 时间类的介绍和使用 其他类型介绍和使用 包装类(Wrapper Class)基本知识: 1.基本数据类型不是对象,但有时需要将 ...

  8. SCSS随笔-mixin与@extend

    变量 定义变量 $color-white: white; 使用变量 body { background-color: $color-white; } @mixin 与 @include 定义mixin ...

  9. GitHub SSH 快速配置

    每次更换系统或者电脑时,都需要重新配置一番 Github SSH 的验证,记性不太好,写了一个快速部署的辅助脚本,直接安装脚本提示使用即可,经测试,Linux 和 Windows 下均能使用. 脚本功 ...

  10. vue4 项目的创建

    1,安装vue cli 脚手架,是全局安装npm install -g @vue/cli 可以在输出信息中看到安装位置,例如:C:\Users\xiaochangjian\AppData\Roamin ...