swig no module named _example
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 开发者_如何学Gothat
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
The file name of the library must be *.pyd. I suppose that you have generated a wrapper code and link it together.
I found that (in windows), if you make a dll it has to be called _modulename.pyd This library (_modulename.pyd), the original c++ module myapp.dll and the resulting modulename.py have to be in the path as well as pythonxx.exe
I found you have to rename the library file that C++ generates from .dll to .pyd on windows. I can't recall if you need to rename it on apple. and your function that you want to expose to python has to have extern "C" preceding it. Otherwise the compiler doesn't make the function accessible outside the library. Also If I recall you need to wrap the return values in a Py_value if you want to use them in python.
精彩评论