问题背景

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. 2019-8-31-C#-转换类型和字符串

    title author date CreateTime categories C# 转换类型和字符串 lindexi 2019-08-31 16:55:58 +0800 2018-2-13 17:2 ...

  2. LVGL 定时器

    LVGL 8.0 以后好像取消了自定义任务模块,想要使用多线程只能使用系统的线程. 一.定时器结构体 typedef struct _lv_timer_t { uint32_t period; // ...

  3. 【图形数据集】CIFAR-10 dataset数据集

    文献引用:https://www.cs.toronto.edu/~kriz/cifar.html The CIFAR-10 dataset The CIFAR-10 dataset consists ...

  4. centos7桌面版安装百度网盘

    百度网盘官网下载Linux版本的软件 CentOS7的软件包格式为rpm # 安装依赖 yum -y install libXScrnSaver yum -y install libstdc++.so ...

  5. 4G EPS 中的 PLMN 选择

    目录 文章目录 目录 前文列表 PLMN 选择 前文列表 <4G EPS 中的系统消息类型> PLMN 选择 UE 开机后的第一件事情就是完成小区搜索,即完成和 eNB 的牵手.在牵手成功 ...

  6. visualstudio着色器设计器shadergraph使用

    第一次使用着色器设计器. vs的着色器设计器是hlsl的着色器设计器.不得不说里面节点得翻译是一坨屎. 附一个光线于法向量夹角渲染的设计图

  7. 珠排序算法C# 简单实现 奇葩排序中的算盘排序(算珠排序)算法

    Console.WriteLine("Hello World!"); int[] arr = { 1, 3, 4, 0, 22, 4,0, 6, 3,10,8,6,7 }; Con ...

  8. MyBatis延迟加载策略详解

    延迟加载就是在需要用到数据的时候才进行加载,不需要用到数据的时候就不加载数据.延迟加载也称为懒加载. 优点:在使用关联对象时,才从数据库中查询关联数据,大大降低数据库不必要开销. 缺点:因为只有当需要 ...

  9. 基于pulp的线性优化问题:微电网日前优化调度(复现)

    摘录来源:(71条消息) 微电网日前优化调度入门:求解一道数学建模题_我不是玉的博客-CSDN博客 学习记录与复现 问题描述 问题出自第十届"中国电机工程学会杯"全国大学生电工数学 ...

  10. docker离线安装

    1. yum安装 #源添加 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo ...