I know this has been asked before but I still can't wrap my head around how this works. I'm trying to do a pretty simple thing which should intuitively work but doesn't, please help me what I'm doing wrong. I have a folder structure like this:
src/
foo/
__init__.py
foo.py
bar/
__init__.py
bar.py
xyz.py
I'm trying to import the XYZ
class in xyz.py
from within foo.py
. If I write
from bar.xyz import XYZ
I get
ModuleNotFoundError: No module named 'bar'
But if I write
from ..bar.xyz import XYZ
I get
ImportError: attempted relative import with no known parent package
I also tried doing
from src.bar.xyz import XYZ
But again I get
ModuleNotFoundError: No module named 'src'
I try adding __init__.py
in src/
but that also results in the same error. I know I can call sys.add
or something and the parent path before importing but that really seems like an hack which I don't want to do. What am I missing here?