CPython: Dynamic module does not define module export function error

喜夏-厌秋 提交于 2021-02-05 08:13:07

问题


I just compiled my Python wrapper for C++ classes successfully. However, I am getting following messages when I am trying to load my module to the Python (through import cell):

ImportError: dynamic module does not define module export function (PyInit_cell)

I checked that the system is using the Python3 on all cases so this is not a Python version problem.
Below is my setup.py file:

from distutils.core import setup, Extension
from Cython.Build import cythonize

setup(ext_modules = cythonize(Extension(
           "cell",                                
           sources=["cell.pyx", "cell.cc"],     
           language="c++",                       
           extra_compile_args=["-std=c++11"],
      )))

Below is the dump for the generated .so file:

0000000000201020 B __bss_start
0000000000201020 b completed.7594
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000000530 t deregister_tm_clones
00000000000005c0 t __do_global_dtors_aux
0000000000200de8 t __do_global_dtors_aux_fini_array_entry
0000000000201018 d __dso_handle
0000000000200df8 d _DYNAMIC
0000000000201020 D _edata
0000000000201028 B _end
0000000000000630 T _fini
0000000000000600 t frame_dummy
0000000000200de0 t __frame_dummy_init_array_entry
0000000000000640 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
00000000000004e8 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000200df0 d __JCR_END__
0000000000200df0 d __JCR_LIST__
                 w _Jv_RegisterClasses
0000000000000570 t register_tm_clones
0000000000201020 d __TMC_END__

I really don't understand why the module is not loaded to the python because there was no error during the building process.

Any help would be appreciated!


回答1:


You should not call your extension/module cell.pyx, call it differently - for example cycell.pyx.

Why? The following steps are taken while extension is built

  1. Cython generates file cell.cpp out of cell.pyx.
  2. Compiler compiles cell.cpp to the object file cell.o.
  3. Compiler compiles cell.cc to the object file cell.o and overwrites the object file created from cell.pyx.
  4. Linker links both cell.o files (but it is in reality only one) - in the result there is nothing what was defined in cell.pyx/cell.cpp in particular PyInit_cell.

By renaming the Cython-file you avoid that the object-file is overwritten.

Clearly, another option would be to rename your c++-file.



来源:https://stackoverflow.com/questions/50719086/cpython-dynamic-module-does-not-define-module-export-function-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!