Python 3.7.3 - ImportError: dynamic module does not define module export function (PyInit_*)

gia . giaym.mail at gmail.com
Sun Jun 30 19:18:23 EDT 2019


Hi, I was trying to upgrade a python 2 extension in Linux and hit this
error upon trying to import it into python 3.

The weird thing is that I can see the symbol when I use readelf -a, so it
is not missing, it has visibility default and it defines as extern "C"
PyObject* return type (using the macro). I build with g++ 6, global
visibility hidden and inline hidden, would that affect it? does it need to
find other functions?

readelf -d shows that a NEEDED library is my altinstall build of python
3.7.3's libs, so that makes me think g++ found the correct library (there
is an oder system library installed). RUNPATH is configured with a relative
path that leads upwards in the folder tree to the python libs. It points to
the same lib folder as the interpreter's RUNPATH, which is also relative.

The module name starts with a underscore, so that's a point of worry, the
standard mentioned ascii, so I assumed underscore is fine, but maybe it
meant alphanumeric. The module is _mymodule_python3, and the init is
PyInit__mymodule_python3, with the *.so being _mymodule_python3.so and a
python file mymodule.py that loads the module inside using 'import
my.package._mymodule_python3'. The cpp file is encoded in utf8, but the
module name string that goes in the module def is not L"", it is plain char.

I load python3 manually and then import this py file and it finds the so
file, but at the end prints the error, I can exit, load another python3 and
import the so directly and same outputs. sys.path doesnt point to the
system install.

I read the standard page and it seems everything is set up correctly
(although it obviously isn't or it should be working).

I've had a lot of difficulty debugging the import call because it is frozen
so I can't just edit the _bootstrap.py file, I edit it and it still uses
the frozen code. I even edited the file in the source tree itself by adding
a few print calls then rebuilt cpython, and they seem to be consumed by the
freeze pass, as nothing gets printed, how do you debug it properly? google
is not helping me.

import is failing at _gcd_import (name, package = None, level = 0) -> find
and load. And if I use a different loading method by importing importlib
(used in documentation page), it manages to obtain the spec which looks
okay upon printing it, but fails upon trying to load and return the module
object with spec.loader.create_module.

Appreciate ideas or a proper debugging method for the import function.



More information about the Python-list mailing list