swig no module named _example

喜你入骨 提交于 2020-01-12 07:42:48

问题


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


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4095083/swig-no-module-named-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!