Understanding importing in Python

Viewed 24

I have checked for similar questions but no one answered my question, which I know that it's quite dumb, but anyway.

Let's say that I have the following:

my_app
    input_module
        data_input.py
        data_processing.py
        __init__.py
    output_module
        output_generation.py
        output_display.py
        __init__.py
    __init__.py
main.py

The __init__.py files are correctly done.

output_generation.py needs to import a class from data_input.py and I do it by writing from input_module import DataInputClass since in the __init__.py I import that class. The thing is that if I do from my_app.input_module import DataInputClass, then the error is that my_app module hasn't been found, which I understand.

But, if then in main.py I do: from my_app.output_module import DataOutputClass, then it will trigger an error in the line from input_module import DataInputClass of output_generation.py saying that input is not found, which I also understand.

My question is how can I fix this The easiest solution is putting main.py inside of my_app but, is there any other way to solve this without doing it? Is there any way to make the imports relative with respect to the file in which the import is done, and not the working directory of the main file?

Sorry if the question is confusing. Thanks!

0 Answers
Related