I have two files, module.c
and submodule.c
.
I have the following code in setup.py:
from distutils.core import setup, Extension
module = Extension('module', sources = ['module.c'])
submodule = Extension('submodule', sources = ['submodule.c'])
setup (name = 'module',
version = '0.1',
description = 'a module that does things',
ext_modules = [module, submodule])
I build it as below:
$ DISTUTILS_DEBUG=1 python3 setup.py build
In the python shell, when I do the following:
>>> import module # works
>>> from module import submodule # this should work
...
ImportError: cannot import name 'submodule' from 'module' (/home/username/Projects/module/build/lib.linux-x86_64-3.8/module.cpython-38-x86_64-linux-gnu.so)
>>> import module.submodule # is this supposed to work?
...
ModuleNotFoundError: No module named 'module.submodule'; 'module' is not a package
>>> import submodule # This should not work
...
ImportError: dynamic module does not define module export function (PyInit_submodule)
Do note that in the last case (import submodule
), my PyInit
function was named PyInit_module_submodule()
, which threw the ImportError
. If I change it to PyInit_submodule()
, then import submodule
works.
I probably have a fundamental misunderstanding of how modules work in Python, so all help is appreciated.