找不到numpy.i

时间:2019-10-22 21:53:48

标签: python c++ numpy wrapper swig

我想使用swig在python中包装一些c ++代码,并且我需要能够使用numpy.i将numpy数组转换为向量。

这是一个令人沮丧的过程,因为我一直无法在线找到有关我实际从numpy.i那里获得的有用信息。

这是我目前正在运行的:

numpy 1.17.3

swig 2.0.12

python 3.7.3

Debian 4.9.2

通过阅读https://docs.scipy.org/doc/numpy/reference/swig.interface-file.html,我被告知numpy.i应该位于tools / swig / numpy.i中,尽管我机器上唯一可以找到numpy.i的地方位于python 2.7文件夹中我已经升级了。我的工作版本的python(3.7.3)没有此类文件。

$ locate numpy.i
/usr/lib/python2.7/dist-packages/instant/swig/numpy.i

我尝试过的事情:

  • 将numpy.i(如上所述)复制到我的工作文件夹中。当我调用%include“ numpy.i”时,至少我的test.i文件可以识别出它,但是它似乎不允许使用numpy.i调用。

  • 将此代码https://github.com/numpy/numpy/blob/master/tools/swig/numpy.i复制到一个名为numpy.i的新文件中,并将其放在我的文件夹中,但是当我尝试运行它时遇到很多错误。

是否有获取正确numpy.i版本的标准方法?我应该在哪里下载,又应该放在哪里?

我在下面提供了一些代码作为参考:

test.i:

%module test
%{
    #define SWIG_FILE_WITH_INIT
    #include "test.h"
%}

%include "numpy.i"  //this doesn't seem to do anything
%init %{
import_array();
%}

%apply (int DIM1) {(char x)};  //this doesn't seem to do anything

%include "test.h"

test.h:

#include <iostream>

void char_print(char x);

test.cpp:

#include "test.h"

void char_print(char x) {
  std::cout << x << std::endl;
  return;
}

tester.py:

import test

test.char_print(5)  #nothing is printed, since this isn't converted properly to a char. 

这只是一个简单的示例,但是我尝试了以多种不同方式使用numpy.i(包括复制和粘贴适用于他们的其他人的代码),但是无论我是否拥有它,始终不变test.i文件与否。

从哪里/如何获得numpy.i?

2 个答案:

答案 0 :(得分:0)

问题::我从python2.7软件包复制过来的numpy.i文件不兼容,并且当您通过anaconda进行安装时,兼容版本也不包含在安装软件包中(仍然不知道为什么会这么做。

答案::找到您正在运行的numpy版本,然后转到此处(https://github.com/numpy/numpy/releases)并下载numpy- [your_version] .zip文件,然后专门复制numpy。我的文件位于numpy- [您的版本] / tools / swig /中。现在将numpy.i粘贴到您的项目工作目录中。

答案 1 :(得分:-1)

您应该从https://github.com/numpy/numpy/blob/master/tools/swig/numpy.i下载新的numpy.i文件。在这个numpy.i文件中没有python3不支持的PyFile_Check函数。如果您仍然使用 /usr/lib/python2.7/dist-packages/instant/swig/numpy.i,您的代码可能会出现错误undefined symbol: PyFile_Check,因为只有python2才能支持PyFile_Check函数。

顺便说一句,当发生错误undefined symbol: PyFile_Check时,SWIG不一定有问题。

相关问题