I have the following folder structure:
[parent_folder]
|-- [main_folder]
|-- main.py
|-- [functions_folder]
|-- module_functions.py
|-- init.py
File contents:
#main.py
from functions_folder.module_functions import function_a
function_a(5)
#module_functions.py
def function_a(inp):
print(inp)
#init.py
from .functions_folder import module_functions
At it's current state, main
cannot load functions from functions_folder
(which makes sense) with the error:
[...]
ModuleNotFoundError: No module named 'functions_folder'
-I know for fact that if main.py
was in parent_folder
the entire thing would run without problems (because it would be able to see one level down).
-I also know that if I add:
sys.path.append(re.search(r'(.*\\)', os.path.dirname(__file__)).group(1))
to main.py
before importing (ie. if I add the parent_folder
to path so that it can now see the functions_folder
one level down), it would again be ok.
But, is there way to achieve this without adding to path, just by using init
s?
In other words, what would the content of the two init
files in the parent_folder
and main_folder
need to be in order for main
to see the functions_folder
content?
EDIT. I believe that the question suggested in comments does not answer the current one because the problem is with imports from same level (but different folder) or higher, not from a folder down.