lllrnr101
lllrnr101

Reputation: 2343

Why can't Main Module see sub-module inside it?

I am editing to minimize to a reproducible example:

Contents of cal.py

import M1

M1.SM1.nice_hello.hello()

Directory structure:

M1/
├── __init__.py
└── SM1
    ├── __init__.py
    └── nice_hello.py

Contents of SM1/nice_hello.py:

def hello():
    print(f'Hello my friend!!!')

All other files (init.py files) are empty.

To run cal.py:

export PYTHONPATH=/PATH/TO/M1 ; python cal.py

But that gives me following error:

Traceback (most recent call last):
  File "cal.py", line 3, in <module>
    M1.SM1.nice_hello.hello()
AttributeError: module 'M1' has no attribute 'SM1'

Upvotes: 2

Views: 310

Answers (2)

Pratap Alok Raj
Pratap Alok Raj

Reputation: 1206

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

Upvotes: 1

ctenar
ctenar

Reputation: 808

It should work if you import the whole module name, i.e. in the file cal.py

import M1.SM1.nice_hello

M1.SM1.nice_hello.hello()

Upvotes: 1

Related Questions