What if "test.py" and "hello.py" in different folders?

In Java, we have classpath to look for the folder to find imported classes. How we do it in Python if the module we want to import is in a different folder?

Hello @janofass,

First of all, a warm welcome from all of us here. Happy to have you in the community. :smile:

Now coming to your question, suppose you have a directory structure like this.

app/ 
 |---__init__.py
 |---sub1/
 |------|__init__.py
 |------|mod1.py
 |------|sub2/
 |---------|__init__.py
 |---------|mod2.py

To import mod2.py in mod1.py, you do import sub2.mod2 in mod1.py file.
Similarly if you want to import mod2.py in __init__.py at the app directory, then you do, import sub1.sub2.mod2.py

Now if you want to import mod2.py in mod1.py then it is a bit complex than importing from a child directory. There is an extensive discussion on packages and imports in python, you can easily google one.
But a good workaround will be to append the parent directory in the sys.path list.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.