问题
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
- Cython generates file
cell.cpp
out ofcell.pyx
. - Compiler compiles
cell.cpp
to the object filecell.o
. - Compiler compiles
cell.cc
to the object filecell.o
and overwrites the object file created fromcell.pyx
. - Linker links both
cell.o
files (but it is in reality only one) - in the result there is nothing what was defined incell.pyx
/cell.cpp
in particularPyInit_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