Packaged Python C Extension is missing PyInit_module() in so File

I have created a C library libgac and then wrote Python module implementing bindings with ctype. I call the python module gazepy.

My project file structure is as follows:

    gac          # repo with the C library libgac    # python bindings for the C library libgac  # empty file

Running sudo pip install . generates and installs the so file

So far so good, however, if I want to import the library in a python3 shell I get the following error:

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

My pyproject.toml file:

requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"

name = "gazepy"
# ...

My file:

from setuptools import setup, Extension, find_packages
import subprocess

module_name = "gazepy"
module_path = "src/" + module_name
gac_path = module_path + "/gac"
cglm_path = gac_path + "/cglm"

def main():
                Extension(module_name, [gac_path + '/src/gac.c'],
                    include_dirs=[gac_path  + '/include', cglm_path + '/include'],

if __name__ == "__main__":

When looking at the so file I cannot find any definition of PyInit_gazepy():

nm /usr/local/lib/python3.8/dist-packages/ | grep PyInit

What am I missing here?


  • python extension modules (.so and .pyd files) have higher priority than .py files when using the import command, so python is trying to import the .so file instead of the .py file.

    in order for import gazepy to correctly import your python file you should change the name of the extension module module_name to have an underscore _gazepy, this way python is not going to load it, then you should only load your .so using ctypes.

    you are compiling your C library as a python extension, which is wrong, as it now depends on python

    the above solution will work although your library is not a python extension module, but you should consider Compiling & installing C executable using python's setuptools/ so it won't depend on python, unfortunately this has worse portability than just treating this C library as an extension module, so if you care about producing a clean library you should try compiling it as a C library instead of an extension module, but if you care about low effort portability then keep your current python extension method.