0

I have made a python wrapping of a c++ class similar to here and here. but the generated python wrapper is only usable in python2 while giving the following error in python3:

dynamic module does not define module export function (PyInit_...)

my setup.py looks like the following:

## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

setup_args = generate_distutils_setup(
    packages=['hw_interface_pkg'],
    package_dir={'': 'src'},
    requires=['std_msgs', 'rospy'],
    language_level="3",
    compiler_directives={'language_level' : "3"}
)

setup(**setup_args)

following this thread I tried adding language_level = "3" or compiler_directives={'language_level' : "3"} to the above setup_args but none solved the problem.

my pyx file also looks like:

cdef extern from "remote_hw.h":
    cdef cppclass ROBOTHardwareInterface:
        ROBOTHardwareInterface() except +
        double c_fibonacci(double n);

cdef class PyHWInterface:
    cdef ROBOTHardwareInterface c_obj
    def __cinit__(self):
        pass
    def fibonnaci_func(self,n):
        return self.c_obj.c_fibonacci(n)

-------------edit

I made sure setup.py is called by python3. but still the same issue.....

Alejandro
  • 879
  • 11
  • 27
  • 1
    That's correct. An extension module built with Python 2 will not be usable on Python 3. You need to rebuild it with Python 3 (i.e. invoke setup.py with Python 3). language_level is to do with how Cython interprets the pyx file so is not related to your problem. – DavidW Jun 17 '20 at 07:33
  • @DavidW I see, the thing is setup.py is invoked automatically during a so called "catkin build" command where it is likely the "catkin_python_setup()" in the CMakeLists.txt that does it. but it's not clear to me how I can make it invoke setup.py with python3. or any other solution/hack... – Alejandro Jun 17 '20 at 07:52
  • @DavidW I made sure setup.py is called by python3.6 but still the same issue presists..... – Alejandro Jun 17 '20 at 10:34
  • What is the output file called? It should be something like `filename.cpython-36.so` (or .pyx). If it doesn't have an extension with the version in it then it probably isn't being called by Python 3.6. I've never heard of catkin before so have no useful advice on that. – DavidW Jun 17 '20 at 11:26
  • @DavidW inside setup.py I had prtint(sys.version), which printed "3.6.9 (default, Apr 18 2020, 01:56:04)". there is a similar issue here: https://stackoverflow.com/questions/8024805/cython-compiled-c-extension-importerror-dynamic-module-does-not-define-init-fu although non of the ideas in it has worked for me so far – Alejandro Jun 17 '20 at 12:40
  • the generated .so files don't have any .cpython ending just hw_interface.so for example – Alejandro Jun 17 '20 at 12:44
  • But how do you call setup.py? My guess is that you still have so from python2 around. – ead Jun 17 '20 at 16:59
  • @ead first I call "catkin build" which in a CMakeLists.txt file applies catkin_python_setup(). Then although it uses python3 to call setup.py, the above issue is there. There are some cython related cmake files which I suspect might be the place for solving the issue https://github.com/longjie/ros_cython_example/tree/master/cmake but I have no clue at all yet – Alejandro Jun 17 '20 at 17:49
  • https://stackoverflow.com/questions/37070304/how-to-build-opencv-for-python3-when-both-python2-and-python3-are-installed? Something like this? It might be you have to specify it with cmake? That's a guess though – DavidW Jun 18 '20 at 08:03

0 Answers0