Submodules are not imported recursively, if you want that to happen you can do the below: -
A) Create init.py file inside M1 module ( I think you already have that )
B) Have the below code in your init.py file : -
import importlib
import pkgutil
def import_submodules(package, recursive=True):
""" Import all submodules of a module, recursively, including subpackages
:param package: package (name or actual module)
:type package: str | module
:rtype: dict[str, types.ModuleType]
"""
if isinstance(package, str):
package = importlib.import_module(package)
results = {}
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
full_name = package.__name__ + '.' + name
results[full_name] = importlib.import_module(full_name)
if recursive and is_pkg:
results.update(import_submodules(full_name))
return results
This will help you import all the submodules inside that package ( M1)
Now in your cal.py do below: -
import M1
M1.import_submodules(M1)
def hello():
print(f'Hello my friend!!!')
Hopefully this will resolve your issue and might guide you on how to import modules recursively in python
Reference :- How to import all submodules?
Please reach out in comments if any further clarification is required. Will be happy to help