Issues with importing from submodules

Viewed 59

I've got a project with the following structure:

\main_model
    __init__.py
    main.py
    \second_module
        __init__.py
        second.py
        \third_module
            __init__.py
            third.py

second_module and third_module are both Git submodules used as libraries by many other projects.

The first line of main.py is an import statement: from main_model.second_module import second And the first line of second.py is too: from second_module.third_module import third

Running main.py raises an error with the following Traceback:

Traceback (most recent call last):
  File "/main_model/main.py", line 1, in <module>
    from main_model.second_module import func_a, func_b
  File "/main_model/second_module/second.py", line 1, in <module>
    from third_module.third import func_c, func_d
ModuleNotFoundError: No module named 'third_module'

So evidently I would need to amend the import statement in second.py to be from main_model.second_module.third_module import third. I can't do that, though, because second_module is a standalone submodule.

Is there a consistent way of structuring the imports, or modifying PYTHONPATH, that would allow the same import syntax from all levels?

0 Answers
Related