Monday, July 13, 2009

ImportError: dynamic module does not define init function

Don't do the same mistake I did...

If you have compiled your own .dll or .so file and when you are trying to import a python file then make sure your dll filed does not have the same name as one of the python files.

For e.g. if your directory contains the following files:
mymodule.dll
mymodule.so
mymodule.py

and you do

>> python
>> import mymodule

You will get the above error and that is because it is trying to import not from mymodule.py but from mymodule.so/dll.

1 comment:

Evgeni Sergeev said...

I'd like to add that I also had this error recently, but for a different reason.

I was surprised at the error, because the init function was there:
$ nm libname.so | grep initlibname
showed it.

It took some time to figure out, but it turns out that the shared library has a special table of functions that it exports - it doesn't export all symbols visible with nm.

The solution was to use the -Wl,-E switch when compiling, to tell the linker to export all symbols. There also seems to be a way to export particular symbols, but I didn't spend any more time - the above worked.


Evgeni Sergeev

--
Here is a website I made, displaying my father's woodwork online: http://nik-sergeev.com