1

I'm wanting to wrap some c++ code in python using swig, and I need to be able to use numpy.i to convert numpy arrays to vectors.

This has been quite the frustrating process, as I haven't been able to find any useful info online as to where I actually get numpy.i from.

This is what I currently have running:

numpy 1.17.3

swig 2.0.12

python 3.7.3

Debian 4.9.2

From reading https://docs.scipy.org/doc/numpy/reference/swig.interface-file.html I'm told that numpy.i should be located in tools/swig/numpy.i, though the only place on my machine that I can find numpy.i is in a python 2.7 folder which I've upgraded from. My working version of python (3.7.3) holds no such file.

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

What I've tried:

  • copying the numpy.i (as described above) into my working folder. This is at least recognized by my test.i file when I call %include "numpy.i", but it doesn't seem to allow usage of numpy.i calls.

  • Copying this code https://github.com/numpy/numpy/blob/master/tools/swig/numpy.i into a new file called numpy.i and putting that in my folder, but I get lots of errors when I try to run it.

Is there a standard way to get the proper numpy.i version? Where would I download it from, and where should I put it?

I've included some code below as reference:

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. 

This is just a simple example, but I've tried using numpy.i in many different ways (including copying and pasting other people's code that works for them) but it consistently doesn't change anything whether I have it in my test.i file or not.

Where/how do I get numpy.i?

Otherness
  • 385
  • 2
  • 16
  • While someone who knows might be able to help you, as a general rule asking where to find things is off-topic on Stack Overflow. That being said, I appreciate you taking the effort to tell us where you have already searched. –  Oct 22 '19 at 21:58
  • You've definitely found numpy.i and copying it into the working directory will work. Likely the issue is that your cout call is printing it as a char, but char(5) isn't a printable character. `std::cout << static_cast(x) << std::endl;` will do what you wanted. – Flexo Oct 22 '19 at 22:08
  • @Chipster Good to know. My point was that I know it's not where it should be, by the use of $ locate numpy.i. I was trying to show that it isn't there, and then asking how I might download it or get it by some other method. – Otherness Oct 22 '19 at 22:29
  • @Flexo But it should be a char, seeing as I used %apply to convert the type, right? My point there is that %apply isn't doing anything (not only in this example, but for many others), and neither is %include "numpy.i". Shouldn't there be a numpy.i in my python 3.7.3 folder for that specific version? – Otherness Oct 22 '19 at 22:29
  • SWIG can't change how your C++ functions themselves behave. It simply controls how you interface those functions from other languages. I.e. it's only concerned with the boundaries of languages, not the internals of either. That behavior with `char` types is a purely C++ thing, your test.cpp file would behave like that even if you called it from C++ rather than Python. – Flexo Oct 22 '19 at 22:36
  • As for where the file is on disk, that's a Debian packaging issue. The path in the documentation is almost certainly talking about where to find it within the numpy source tarball. – Flexo Oct 22 '19 at 22:40
  • Again, this is just one of many type conversions that don't do anything. Every time I use %apply in the way the numpy.i document advices, it doesn't do anything. It just seems strange that I don't have a numpy.i in my python 3.7.3 folder and that I have to use numpy.i from python 2.7. – Otherness Oct 22 '19 at 22:43

2 Answers2

1

Problem: The numpy.i file I copied over from the python2.7 package isn't compatible, and the compatible version isn't included in the installation package when you go through anaconda (still not sure why they'd do that).

Answer: Find which version of numpy you're running, then go here (https://github.com/numpy/numpy/releases) and download the numpy-[your_version].zip file, then specifically copy the numpy.i file, found in numpy-[your_version]/tools/swig/. Now paste that numpy.i into your project working directory.

Otherness
  • 385
  • 2
  • 16
-1

You should download new numpy.i file from https://github.com/numpy/numpy/blob/master/tools/swig/numpy.i. In this numpy.i file have no PyFile_Check function, which python3 don't support. If you still use /usr/lib/python2.7/dist-packages/instant/swig/numpy.i, your code may appear error undefined symbol: PyFile_Check because only python2 can support PyFile_Check function.

By the way, when the error undefined symbol: PyFile_Check occurs, it is not necessarily a problem with SWIG.

Shizzen83
  • 3,325
  • 3
  • 12
  • 32