Apologies for yet another "why can't I import my python function" question. I'm not happy about it either.
I have a python project with the following structure:
~/my-repo tree my_package
my_package
├── __init__.py
├── main.py
└── utils.py
├── notebooks
│ ├── __init__.py
│ └── notebook.ipynb
├── tests
│ ├── __init__.py
│ └── integration_test.py
│ └── unit_tests.py
utils.py contains the following import and function:
import pandas as pd
def util_func():
ts = pd.Timestamp.now()
return ts
I can successfully import util_func in main.py and in unit_tests.py. I can also successfully run main.py from the shell, and run pytest from the shell (from my-repo) which successfully imports util_func to test it.
But in my-repo/notebooks/notebook.ipynb, I cannot import util_func from utils.py. I know in the notebook, my package is not installed, so presumably I need some sort of relative import for it, but I can't get it to work. Some things I have tried:
from ..utils import util_funcfrom ..my_repo.utils import util_funcfrom utils import util_func- adding the
__init__.pyfile to thenotebooks/directory per many SO answers to similar questions - moving
notebooks/folder frommy-repo/my_package/notebooks/up tomy-repo/notebooks/ - read answers to this related SO question and this one and this one and others
- (UPDATE) adding
from sys import path&path.append("../")
When I type from ..utils import util_func, my IDE even auto-completes and the colors of utils and util_func indicate they're found. But then when I execute the cell I always get either ImportError: attempted relative import with no known parent package or ModuleNotFoundError.
I don't want to move the notebook.ipynb into the top-level my-repo/ directory because overtime we will spawn more and more exploratory notebooks and I don't want them cluttering the top-level directory, but I need to be able to import our util functions running notebooks interactively.
What do I need to change, in either notebook.ipynb or my package/folder structure, to allow me to import util_func from utils.py in my notebook (and without breaking my ability to run python -m my_package.main and pytest successfully from $my-repo/)