Dynamically create modules inside __init__ if they don’t exist

I would like to dynamically create and import modules inside an inner __init__.py file, if one or several of a set of indexed submodules doesn’t exist.

I have a set of module layers, say;

top_module/
    __init__.py
    sub_module/
        __init__.py
        a1/
            __init__.py
            n1.py
            b1/
                __init__.py
                b1.py
            b2/
                __init__.py
                b2.py
            b3/
                __init__.py
                b3.py
        a2/
            __init__.py
            a2.py
            b*/...
        a*/...

Where the top_module/__init__.py does a from .sub_module import *.

From within the top_module/sub_module/__init__.py, say I have several of these a* folders that are just indexed iteratively. I know that I can do something like this to iterate importing over an index for those modules that exist;

from importlib import import_module

for a in range(some_max_a):
    import_module(f'.a{a}', package='top_module.sub_module')

And that I can do something like this to just ignore modules that don’t exist yet;

from importlib import import_module

for a in range(some_max_a):
    try:
        import_module(f'.a{a}', package='top_module.sub_module')
    except ModuleNotFoundError:
        pass

What I would like to be able to do is dynamically create and import these modules if they don’t exist.

What I have so far is

from importlib import import_module
from sys import modules
from types import ModuleType

PKG = 'top_module.sub_module'

for a in range(some_max_a):
    try:
        import_module(f'.a{a}', package=PKG)
    except ModuleNotFoundError:
        modules[f'{PKG}.a{a}'] = ModuleType(f'{PKG}.a{a}')
        for b in range(some_max_b):
            modules[f'{PKG}.a{a}.b{b}'] = ModuleType(f'{PKG}.a{a}.b{b}')
            def function_all_should_have(*args, **kwargs):
                raise NotImplementedError
            modules[f'{PKG}.a{a}.b{b}'].function_all_should_have = function_all_should_have
        import_module(f'{PKG}.a{a}')

I’ve tried with and without the {PKG} in the import_module call and or the ModuleType call.

If I import the created package, I can see that all the modules that I’m expecting this to create exist in the sys.modules, but trying to access any of them with a call to something like top_module.sub_module.a3.b3.function_all_should_have() yields an error along the lines of

AttributeError: module 'top_module.sub_module' has no attribute 'a3'.

Yet I can see that there is a top_module.sub_module.a3 module along with all the top_module.sub_module.a3.b* modules.

I’m not really sure why the modules would be created and exist in sys.modules but be unreachable after importing.

If there’s no easy answer I could just go back to my second example and pass if the modules don’t exist, but I would still like to understand what’s happening here. The only closest question I could find to this was dynamic module creation.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật