ImportError: dynamic module does not define init function, but it does
Asked Answered
V

8

10

I'm trying to write a binding for a vendor C++ library. I've successfully used snippets such as the below to define init functions in the other modules, but in this one it doesn't seem to work: it compiles fine, but throws the ImportError as soon as I try to import it into a test script. What could be wrong here?

#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC initclient(void) {

    PyObject* m;

    ClientType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&ClientType) < 0)
        return;

    m = Py_InitModule3("client", client_methods, "Client module");
    Py_INCREF(&ClientType);
    PyModule_AddObject(m, "Client", (PyObject *) &ClientType);

}

This is on 32-bit Linux, with gcc 4.4.4.

Villein answered 18/2, 2011 at 13:29 Comment(0)
V
-3

This turned to be unrelated to Python or the compiler, but was an incorrect compiler incantation (have to pay more attention while editing the Makefile).

Villein answered 18/2, 2011 at 14:5 Comment(3)
it would be great if you could include your solution for other people who have the same problem!Forehead
@Villein any extra info on you're solution? I'm stuck on this problem currentlyUnprofessional
Sorry, I don't remember what the actual issue was, and I can't find relevant changes in my version control repository.Villein
G
8

I had the same issue. At compile time:

  • path to the Python header: OK
  • path to the Python library: OK
  • link against the Python library: OK
  • link against needed third parties libraries/object files: OK

I just forgot to compile the C file that defines my module... Sigh...

So yeah, first thing to check: your makefile or your compilation command! :)

Gaillardia answered 14/5, 2011 at 15:49 Comment(1)
You saved my day! I wrote my build script walk though all the src code path, but forget the main file.c, ...sighPenneypenni
G
8

Make sure you don't mix Python versions. In Python version 2 the init function was called Init_, while in version 3 this function is called PyInit_

In my case this was happening when SWIG 3.0.2 used Python 3.4 to generate bindings, while my Python IDE called the Python 2.7 interpreter.

You can see the difference in the generated .cxx file:

#if PY_VERSION_HEX >= 0x03000000
#  define SWIG_init    PyInit__<modulename>

#else
#  define SWIG_init    init_<modulename>

#endif

On linux you can also use the following command to check your .so exports:

nm -D <modulename> | grep <modulename>

This will give you the name of the init function within your library.

Gilolo answered 4/12, 2014 at 12:26 Comment(1)
The answer is spot on. Since I use cmake to automatically generate everything using SWIG all I had to change was find_package(PythonLibs 2.7 REQUIRED) in order to specify which version I want because the default (without version) was giving me the 3.5 and I landed in the situation described in this answer.Mesne
O
5

I had the same error message, but it was because I renamed my .c file, and forgot to update the name inside the code. The "initxxx" function and an argument inside it.

Orsay answered 11/3, 2013 at 17:17 Comment(0)
T
1

Make sure you include your _wrap.cxx. It seems to me it doesn't get compiled into your module.

Tumult answered 18/8, 2013 at 19:23 Comment(0)
I
0

On linux it can help to run strace in this case. Check that the name of the library python is searching for is the same as the name of the library you built.

Inerrable answered 21/3, 2013 at 7:43 Comment(0)
A
0

The swig documentation mentions here:

This error is almost always caused when a bad name is given to the shared object file. For example, if you created a file example.so instead of _example.so you would get this error.

Asperges answered 6/6, 2013 at 19:17 Comment(0)
A
0

In the interface file, SWIG suggests using:

#define SWIG_FILE_WITH_INIT
Arondell answered 20/4, 2015 at 18:17 Comment(0)
V
-3

This turned to be unrelated to Python or the compiler, but was an incorrect compiler incantation (have to pay more attention while editing the Makefile).

Villein answered 18/2, 2011 at 14:5 Comment(3)
it would be great if you could include your solution for other people who have the same problem!Forehead
@Villein any extra info on you're solution? I'm stuck on this problem currentlyUnprofessional
Sorry, I don't remember what the actual issue was, and I can't find relevant changes in my version control repository.Villein

© 2022 - 2024 — McMap. All rights reserved.