Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swig no module named _example

Tags:

c++

python

gcc

swig

I cannot reproduce the basic SWIG example on windows. My error is stated in the SWIG docs and I am sure that I do the 2 fixes they mention. For this error:

>>> import example
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "example.py", line 2, in ?
    import _example
ImportError: No module named _example

the SWIG documentation clearly states:

forget the leading underscore (_).

forget the leading underscore (_).> If you get this message, it means that

you either forgot to compile the wrapper code into an extension module or you didn't give the extension module the right name. Make sure that you compiled the wrappers into a module called example.so. And don't forget the leading underscore ().forget the leading underscore (_).

and I am sure that I link with the latest wrap object build and I have tryied: "_example", "_example.so", "example.dll", "example.so", "example.dll", even all at once, and that the generated "example.py" is in the same folder as the shared library, and that the python path contains this directoryforget the leading underscore ().

THE EXAMPLE:

//example.h
int foo_sum(int a, int b);

.

//example.cpp
int foo_sum(int a, int b) {
    return a + b;
}

.

//example.i
%module example
%{
#include "example.h"
%}

#include "example.h

and the build commands:

gcc -IV:\temp\example\external\include\Python -O3 -Wall -c -fmessage-length=0 -oexample_wrap.o ..\example_wrap.c
g++ -IV:\temp\example\external\include\Python -O3 -Wall -c -fmessage-length=0 -oexample.o ..\example.cpp
g++ -LV:\temp\example\external\lib -shared -oexample.dll example_wrap.o example.o -lpython26

even if I don't use -O3 it still doesn't work (I pasted the build commands from a Release configuration)

I also tried this and to no success:

>>> import sys
>>> sys.path.append("/your/module/path")
>>> import example

EDIT:

apparently it loads the dll if you rename it to "_example.pyd", BUT the module loaded does not contain my "foo_sum" function

EDIT: it works now, I am using extern "C" and not including headers in the .i file

like image 932
lj8888 Avatar asked Nov 04 '10 08:11

lj8888


People also ask

How do I use swig in Python?

The SWIG %module directive specifies the name of the Python module. If you specify `%module example', then everything is wrapped into a Python 'example' module. Underneath the covers, this module consists of a Python source file example.py and a low-level extension module _example.so.

What is swig object Python?

The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme.

What is swig file?

SWIG is a software development tool that simplifies the task of interfacing different languages to C and C++ programs. In a nutshell, SWIG is a compiler that takes C/C++ declarations and creates the wrappers needed to access those declarations from other languages including Perl, Python, Tcl, Ruby, Guile, and Java.


1 Answers

The file name of the library must be *.pyd. I suppose that you have generated a wrapper code and link it together.

like image 73
jhruby Avatar answered Sep 20 '22 22:09

jhruby