(Swig to python) import error:dynamic module does not define init function

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. 我很高興幫助你!

Tags:

Python

C++

Swig