Unable to solve "ImportError: dynamic module does not define module export function"
Question:
The is the link to the python package I am trying to compile and install. I have tried what I can find online for hours but cannot get over the ImportError
.
The package has the following contents.
Its setup.py has the following content. There are two modules here. One is the python wrapper package with sparse_learning
, the other is a c extension module named proj_module
.
I followed the procedure described here https://docs.python.org/3.6/extending/building.html to compile and install on Ubuntu 18.04. There is no error message.
sudo python3 setup.py build_ext –inplace
sudo python3 setup.py install
Then when I try to load the C-extension module proj_module
, an error “ImportError: dynamic module does not define module export function” occurs.
python3 -c “import proj_module”
I tried to apply solutions found online, including uninstalling Python2 with sudo apt purge python2.7-minimal
, or add python3 site-packages paths to the bashrc. However, none of them worked.
I just know it was originally written for Python 2. Then two modifications are made in the main_wrapper.c
for it to run for Python 3. They look correct to me…
Answers:
It looks like you’ve got a little bit of Python 2-style code mixed in with your Python 3 module here. You just need to replace
PyMODINIT_FUNC initproj_module() {
with
PyMODINIT_FUNC PyInit_proj_module() {
in your main_wrapper.c
file.
Maybe this official document can be helpful; it works for me.
Just follow the steps shown in the picture: this pic shows how to address it
If you want to know more about it, please look up the official document, which can be found here:
https://learn.microsoft.com/en-us/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2022
This error can also occur when you’re using a build system like Cython and CMake. In PyInit_<modname>
, modname
should match the filename. For example, you might ask Cython to create the extension
target, but CMake may produce libextension.so
from that. When you then import libextension
in Python, it will look for PyInit_libextension
but Cython will have generated PyInit_extension
.
The is the link to the python package I am trying to compile and install. I have tried what I can find online for hours but cannot get over the ImportError
.
The package has the following contents.
Its setup.py has the following content. There are two modules here. One is the python wrapper package with sparse_learning
, the other is a c extension module named proj_module
.
I followed the procedure described here https://docs.python.org/3.6/extending/building.html to compile and install on Ubuntu 18.04. There is no error message.
sudo python3 setup.py build_ext –inplace
sudo python3 setup.py install
Then when I try to load the C-extension module proj_module
, an error “ImportError: dynamic module does not define module export function” occurs.
python3 -c “import proj_module”
I tried to apply solutions found online, including uninstalling Python2 with sudo apt purge python2.7-minimal
, or add python3 site-packages paths to the bashrc. However, none of them worked.
I just know it was originally written for Python 2. Then two modifications are made in the main_wrapper.c
for it to run for Python 3. They look correct to me…
It looks like you’ve got a little bit of Python 2-style code mixed in with your Python 3 module here. You just need to replace
PyMODINIT_FUNC initproj_module() {
with
PyMODINIT_FUNC PyInit_proj_module() {
in your main_wrapper.c
file.
Maybe this official document can be helpful; it works for me.
Just follow the steps shown in the picture: this pic shows how to address it
If you want to know more about it, please look up the official document, which can be found here:
https://learn.microsoft.com/en-us/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2022
This error can also occur when you’re using a build system like Cython and CMake. In PyInit_<modname>
, modname
should match the filename. For example, you might ask Cython to create the extension
target, but CMake may produce libextension.so
from that. When you then import libextension
in Python, it will look for PyInit_libextension
but Cython will have generated PyInit_extension
.