'How can I make a PYTHON submodule directory with similar filenames as those in root import their OWN files with same names first

The title of this question may seem very confusing and I didn't know how to word it properly, but here's the gist of the situation.

Say I have this kind of file structure:

- main.py
- util.py
- submodules
  - module_folder
    - sub_main.py
    - util.py

main.py

import submodules.module_folder.sub_main

util.py

print("Util in root")

submodules/module_folder/sub_main.py

import util

submodules/module_folder/util.py

print("Util in submodules.module_folder.util")

The question is: How can I make submodules/module_folder/sub_main.py's import util load submodules/module_folder/util.py instead of util.py in the root of the directory?

Restrictions:

  • I can't change any files in module_folder. They are to be readonly

Application Context: The reason that I want to build it this way is because I am building a feature that will let the user import arbitrary git submodules which can have random file structures and paths.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source