What is the init function of a dynamic module in python?
Question: I am getting the same error of these other two questions: [ImportError:
dynamic module does not define init function, but it
does](http://stackoverflow.com/questions/5041861/importerror-dynamic-module-
does-not-define-init-function-but-it-does) and [Cython compiled C extension:
ImportError: dynamic module does not define init
function](http://stackoverflow.com/questions/8024805/cython-compiled-c-
extension-importerror-dynamic-module-does-not-define-init-fu)

But their solutions are not equal, and didn't work for me as well.

I am trying to call functions of a shared library that I have wrote in c,
inside my python program.

I compiled my shared lib like this:

    
    
    gcc -shared -Wl,-soname,playfaircrack.so -o playfaircrack.so -fPIC playfaircrack.c scoreText.o
    

I created a module, and inside this module I load this lib with:

    
    
    cracker = ctypes.cdll.LoadLibrary('./playfaircrack.so')
    

But when I run the code, I get the following error:

    
    
    Traceback (most recent call last):
      File "playfair.py", line 2, in <module>
        import playfaircrack
    ImportError: dynamic module does not define init function (initplayfaircrack)
    

Which is very strange, because if I run the python interpreteer, and call
directly:

    
    
    cracker = ctypes.cdll.LoadLibrary('./playfaircrack.so')
    

I can access the functions of my shared lib.

Any ideas how to solve this? Thank you.


Answer: Delete the line

    
    
    import playfaircrack
    

in `playfair.py` and it should work.

Alternatively, rename `playfaircrack.so` to something else or moved it to a
different directory. Python gets confused if you have two files with the same
module name, i.e. `playfaircrack.py` and `playfaircrack.so` in the same
directory. Python tries to import `playfaircrack.so`, which is no valid Python
module, before it gets to `playfaircrack.py`.

