0

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 inits?

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.

Tony
  • 781
  • 6
  • 22
  • Does this answer your question? [Import module from subfolder](https://stackoverflow.com/questions/8953844/import-module-from-subfolder) – Saif Asif Feb 12 '20 at 10:43
  • @Saif Asif I believe that it does not because in this case the imports are from folders below the level of `main`. But if I am thick and it can be easily inferred from this post what is the solution in my case, I would appreciate if you posted the answer. – Tony Feb 12 '20 at 10:48

0 Answers0