1

I'm quite new to python that I always use by writing script in spyder and running them in its Ipython console with python3.6.2 .

I'm trying to write a simple module from a "swig_example.c" file following a couple of swig tutorial (http://www.swig.org/tutorial.html, http://www.swig.org/Doc1.3/Python.html#Python_nn6).

My aim is to be able to run a script "main_python.py" which should look like:

import swig_test

print(swig_test.fact(4))

where fact is a function defined in the original c source.

The source file is "swig_example.c":

/* File: swig_example.c */
#include "swig_example.h"

int fact(int n) {
 if (n == 0) {
     return 1;
 }
 else {
     return n * fact(n-1);
 }
}

The header file is as simple as:

/* File: swig_example.h */

int fact(int n);

and the interface one:

/* File: swig_example.i */
%module swig_test

%{
#include "swig_example.h"
%}

%include "swig_example.h"

When in the terminal I run:

swig -python swig_example.i

a "swig_example_wrap.c" and a "swig_test.py" files are created.

How should I proceed to have my "main_python.py" working? (It now returns a "No module named '_swig_test' error). I would like to have some script (maybe using distutils?) so that each time I modify the .c source I can easily update the module without changing the "main_python.py" file. If you have any solution which uses Xcode instead of spyder it would be well accepted.

I think that the question could be useful to many that are new to python (and Mac actually...) and try to use it while not throwing away their previous works...

EDIT: I -partially- solved the problem. Now the main point remain Spyder. I create the files ".c", ".h" and ".i" the way I described. Then, following this post (Python.h not found using swig and Anaconda Python) I create, in the same folder, my "setup.py" file:

from distutils.core import setup, Extension


example_module = Extension('_example', sources=['example.c','example.i'])

setup(name='example', ext_modules=[example_module], py_modules= .["example"])

Then, In anaconda navigator I open the terminal of the environment I'm working in, move to the right folder and run:

python setup.py build_ext --inplace

If now I open spyder everything works the desired way. But If I now want to modify my C source, say add a new function, problems arises. I modify the ".c", ".h" and ".i" files annd thenn re-run in the terminal the previous line. The "example.py" file result to e correctly modified (it innncludes the attribute of the new function), but when try to import the module in spyder (import example) changes are nnot registered and an error message "_example has no attribute "new function" is given in the Ipython console unless I restart Spyder itself.

Is there faster way to fix it? (maybe this is the interaction mentioned in the comments... )

Thank you all :-)

acini
  • 49
  • 1
  • 6
  • (*Spyder maintainer here*) I think what you want is possible with Cython but not with Swig. Besides, Spyder has native support to run Cython files, which is a plus. – Carlos Cordoba Jan 07 '19 at 18:48
  • Thank for your answer:) ! Can I ask you little more explanation? How is swig supposed to be used? And what's the difference among swig and cython? Maybe I'm not getting how to use swig properly... And would Cython allow me to simply re-use may C-libraries? – acini Jan 08 '19 at 17:55
  • I'm not no expert on that front, but what I know is that both Swig and Cython allow you to easily create Python bindings to existing C/C++ libraries. The advantage of Cython is that you can run it in IPython and so you can create your bindings iteratively and much faster due to that. – Carlos Cordoba Jan 08 '19 at 18:09
  • Indeed the proble holds with pyx files too. If instead of using pyximport.install() in my python script that I run in spyder I call "python setup ..." from the environment terminal the behaviour is not updated. – acini Jan 10 '19 at 10:47

0 Answers0