1

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_func
  • from ..my_repo.utils import util_func
  • from utils import util_func
  • adding the __init__.py file to the notebooks/ directory per many SO answers to similar questions
  • moving notebooks/ folder from my-repo/my_package/notebooks/ up to my-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/)

Max Power
  • 8,265
  • 13
  • 50
  • 91

1 Answers1

0

You must set relative path in order to do that

from sys import path
path.append("../")

This will set path to parent directory now

from utils import util_func

Now module will be sucessully imported

  • 1
    thanks for the suggestion but this did not work for me. after your sys/path code, `from utils import util_func` throws `ModuleNotFoundError`, and `from ..my_package.utils import util_func` throws `ImportError: attempted relative import with no known parent package` – Max Power Mar 05 '22 at 12:12