Okay, pictured are the two MWE's and their failure messages.
In words, I can't naturally import, from the same subdirectory, into a module that I want to run as main for testing purposes. That's the first screenshot.
The next screenshot shows that I cannot naturally import, from the project root, into a subdirectory module that I am similarly running as main for testing purposes.
Visuals:
What I don't want to do is hack my way through like I've been doing in all my projects with lines such as:
if __name__ == '__main__:
import sys
sys.append('..')
This is inelegant because it doesn't withstand changing the folder structure.
I've also tried putting in blank __init__.py
's into the subdirs and also tried putting in those the lines such as:
from .e import E
Nothing easy seems to work. It's really a bummer when I can't handle basic file structures when importing things.
So what is the general fix that solves both of these issues for good and forever?
Text Code:
<proj root>/d.py
:
class D:
pass
<proj root>/root.py
:
from b.a import A
print("import from project root main")
<proj root>/b/a.py
:
from b.c import C
class A(C):
pass
if __name__ == '__main__':
print('import from project subdir')
<proj root>/b/c.py
:
class C:
pass
<proj root>/b/e.py
:
from d import D
class E(D):
pass
if __name__ == '__main__':
print("e can't import something from root")
First set entry point to a.py
and for the other error set entry point to e.py
.
Thank you!