I have a project which is structured as below:
/project
/src
__init__.py
my_module.py
/notebooks
my_notebook.ipynb
I wanted to import my_module.py
in my_notebook.ipynb
, so I first tried import src.my_module
getting ModuleNotFoundError: No module named 'src'
and from ..src import my_module
getting ImportError: attempted relative import with no known parent package
. From several similar topics I found that adding /project
location to SYSPATH would be a quick hack and it works indeed:
import sys
sys.path.insert(1, '/project')
from src import my_module
However, interfering with SYSPATH seems to be a nasty way and I wonder - does a safer/cleaner solution exists. Also, I don't quite get why from ..src import my_module
doesn't work in the first place?