4

I know this might sound simple but I have been wracking my brain for a while now with no resolution.

I have a python script structured as follows:

app
 └── my_resource.py
 └── utils
       └── my_file.py
       └── exceptions
                 └──my_exception.py

Both my_resource.py and my_file.py need to import my_exception.py.

As per the structure, I am importing as shown below:

my_resource.py :

from utils import my_file as my_file
from utils.exceptions import my_exception as my_exception

my_file.py :

from exceptions import my_exception as my_exception

As per my understanding, as I am not importing from a different folder, the import must work. But, I get the following error every time I run my_resource.py.

Traceback (most recent call last):
  File "my_resource.py", line 9, in <module>
    from utils import my_file as my_file
  File "my_file.py", line 6, in <module>
    from exceptions import my_exception as my_exception
ModuleNotFoundError: No module named 'exceptions'

But, when I remove from exceptions import my_exception as my_exception from my_file.py module, my_resource.py runs successfully.

Why is this the case? Is this because there is some form of nested import here [even then why should there be an import error]?

Javad
  • 2,033
  • 3
  • 13
  • 23
mang4521
  • 742
  • 6
  • 22

0 Answers0