I have a Python 3.9.2 project with the following directory structure:
lib/
├─ mod1.py
├─ mod2.py
├─ __init__.py
main.py
In /main.py
, I have from lib import mod1
. In /lib/mod1.py
, I have import mod2
. When I run /main.py
, I receive the following error:
Traceback (most recent call last):
File "/main.py", line 1, in <module>
from lib import mod1
File "/lib/init.py", line 1, in <module>
import mod2
ModuleNotFoundError: No module named 'mod2'
Why is this happening? When I change the code in /lib/mod1.py
to say from lib import mod2
, the code works fine, but I don't understand why that fixes it. Shouldn't I be able to import /lib/mod2.py
the way I originally tried, since both mod1.py
and mod2.py
are in the same directory?