问题
I have written a library in C. I use SWIG to generate bindings for Java, Python, etc. I managed to write custom type maps for both languages and so on. I also managed to use my library (a custom protocol) and communicate with a server that I wrote in C, with a client that I wrote in Java and, with a client that I'm writing in Python.
Recently, I came across a multiple inheritance problem with a solution I found smart. But, when trying to replicate the error using Python 3, the error was gone (maybe in version 3 is solved). The fact is that, the same code, the same project and the same source won't run invoking python3
binary, but it works invoking python2.7 binary.
I get the message:
ImportError: dynamic module does not define init function (PyInit__pytellapic)
Which I already read what could mean from SWIG documentation with a slightly different error:
import example
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: dynamic module does not define init function (init_example)
Saying that:
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. Alternatively, this error could arise if the name of the module is inconsistent with the module name supplied with the %module directive. Double-check the interface to make sure the module name and the shared object filename match. Another possible cause of this error is forgetting to link the SWIG-generated wrapper code with the rest of your application when creating the extension module.
Honestly, I think that this probable causes won't apply to my modules, as it should be an error for 2.7 and 3 versions of python, not just for Python 3.
I would appreciate any advice, but considering that SWIG seems to be an outdated project, probably I'll continuing using Python 2.7 with the mentioned "hack".
Best regards,
回答1:
Does the C module generated have a PyInit__pytellapic
function defined?
The name and profile if the module init method has changed in Python 3. If you want the exact same C-code to run under both Python 2 and Python 3 you must include both the old and the new name. See the Migrating C Extensions chapter for more info.
According to SWIG's documentation SWIG 2.0 should do this if you pass the -py3 parameter, but I haven't tried it.
来源:https://stackoverflow.com/questions/6845869/swig-and-python3-import-error