I was hacking over the weekend and happened across a strange issue. For some reason I Cant import modules from a sibling directory. I have no issues importing from a parent:
| src/
| ---- __init__.py
| ---- lib/
| -------- __init__.py
| -------- MyModule1.py
| -------- MyModule2.py # <--- I have no issues running `from src.lib.MyModule1 import MyModule1`
| ---- test/
| -------- __init__.py
| -------- MyTestModule1.py # <--- I cannot run `from src.lib.MyModule1 import MyModule1`
I have two modules in my lib directory, MyModule
and MyModule2
, and a test in my test dir. Modules in lib
can use other modules as long as they have the same parent (lib/
) but for some reason, modules in lib's sibling test/
cannot use modules in lib/
.
When I try to run MyTestModule.py
:
from src.lib.MyModule1 import MyModule1
I get this error: ImportError: No module named 'src'
.
I do not get this error while running the same code in lib/MyModule2.py
What is going on here? Please help.