I am trying to port my c++ code to python by swig.
When I finish building the py, pyd, cxx and lib files, under Python (command line), I key in "module Dnld", it shows-> import error:dynamic module does not define init function. The following is my code,
Further: Add my build step to avoid misunderstanding, thank you Mark Tolonen
(Enviroment:XP SP3 with VC2008)
//DownloaderEngine.h
class __declspec(dllexport) CDownloaderEngine
{
public:
CDownloaderEngine();
virtual ~CDownloaderEngine();
signed char OpenPort(signed char _ucPort, unsigned long _ulBaudRate, unsigned char _ucParity,
unsigned char _ucStopBits,unsigned char _ucData);
....
};
//DownloaderEngine.cpp
CDownloaderEngine::CDownloaderEngine()
{
....
}
CDownloaderEngine::~CDownloaderEngine()
{
....
}
//DownloaderEngine.i
%module Dnld
%include <windows.i>
%include <std_vector.i>
%include <std_map.i>
%{
#define SWIG_FILE_WITH_INIT
#include ".\SerialComm\DownloaderEngine.h"
%}
/* Parse the header file to generate wrappers */
%include ".\SerialComm\DownloaderEngine.h"
Not really enough information, because the problem is likely in how you are building it. for example, with the files you've specified, building from a VS2008 command prompt should be something like:
swig -python -c++ DownloaderEngine.i
cl /LD /W4 /Fe_Dnld.pyd /Ic:\Python27\include downloaderEngine_wrap.cxx -link /LIBPATH:c:\Python27\libs DownloaderEngine.lib
Edit: Your build steps look about right, but one thing is the .pyd file is expected to be named _Dnld.pyd
(note the underscore).
The generated Dnld.py
calls import _Dnld
(the .pyd), so you will import Dnld
(the .py) in your Python script.
Example:
>>> import Dnld
>>> engine = Dnld.CDownloaderEngine()
>>> result = engine.OpenPort(...)
This is the error I get if I rename the .pyd without an underscore:
>>> import Dnld
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initDnld)
So I'm sure this will fix your issue. 我很高興幫助你!
For the record, here another possible cause of the error message
ImportError: dynamic module does not define init function (init<mylibrary>):
Running Python2 while Swig was set up for Python3, or vice versa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With