I want to override a module of an existing Python application. The application is structured in modules and submodules and I have registered an extension. Within the extension I want to provide a module (new_module.py) which overrides the original module (module.py). So whenever another module imports it, my version is used.
/application
__init__.py
folder_a
folder_b
__init__.py
folder_b_a
__init__.py
module.py
/extension
__init__.py
new_module.py
I guess this can be achieved by setting it in the sys module, like this:
import extension.new_module
sys.modules["application.folder_b.folder_b_a.module"] = extension.new_module
But I am not sure where to put those lines. I tried it in all of the init files, but it does not work. Or is there another way?