1

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.

gny-001f2
  • 29
  • 5
  • `submodule = Extension('module.submodule', sources = ['submodule.c'])` See https://stackoverflow.com/a/58131417/7976758 – phd Aug 06 '20 at 19:00
  • That did not work. Now only ```import submodule``` works, while ```from module import submodule``` still does not work, and neither does ```import module.submodule```. – gny-001f2 Aug 06 '20 at 19:33
  • Do you want to create a package? How would you do what you want to do with normal py-files? – ead Aug 09 '20 at 15:14
  • To be honest, I don't know. I haven't made packages with normal python files. Just simple single-file modules that were straightforward to import. – gny-001f2 Aug 09 '20 at 15:16

0 Answers0