I have a python modules written in C, it has a main module and a submodule(name with a dot, not sure this can be called real submodule):
PyMODINIT_FUNC initsysipc(void) {
PyObject *module = Py_InitModule3("sysipc", ...);
...
init_sysipc_light();
}
static PyTypeObject FooType = { ... };
PyMODINIT_FUNC init_sysipc_light(void) {
PyObject *module = Py_InitModule3("sysipc.light", ...);
...
PyType_Ready(&FooType);
PyModule_AddObject(module, "FooType", &FooType);
}
The module is compiled as sysipc.so
, and when I put it in current directory, following import works without problem:
import sysipc
import sysipc.light
from sysipc.light import FooType
The problem is I want to put this module inside a namespace package, the folder structure is like this:
company/
company/__init__.py
company/dept/
company/dept/__init__.py
company/dept/sys/
company/dept/sys/__init__.py
company/dept/sys/sysipc.so
all the three __init__.py
just includes the standard setuptool
import line:
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
in current directory, following imports does not work:
from company.dept.sys import sysipc;
from company.dept.sys.sysipc.light import FooType;
How should I import the types and methods defined in module sysipc.light
in this case?
===================================
Update with the actual error:
I have sysipc.so
built, if I run python in current directory as this module, import will work as expected:
[root@08649fea17ef 2]# python2
Python 2.7.18 (default, Jul 20 2020, 00:00:00)
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysipc
>>> import sysipc.light
>>>
If however if I put it into a namespace folder, like this:
company/
company/__init__.py
company/dept
company/dept/__init__.py
company/dept/sys
company/dept/sys/sysipc.so
company/dept/sys/__init__.py
import the submodule will not work:
>>> from company.dept.sys import sysipc
>>> from company.dept.sys import sysipc.light
File "<stdin>", line 1
from company.dept.sys import sysipc.light
^
SyntaxError: invalid syntax
>>> from company.dept.sys.sysipc import light
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name light
>>>
The module is built with this simple code, it is for python2. I also have same example for python3.