No module names 'src' when importing from parent folder in jupyter notebook

Question:

I have the following folder structure in my project

my_project
  notebook
    |-- some_notebook.ipynb
  src
    |-- preprocess
        |-- __init__.py
        |-- some_processing.py
    __init__.py

Now, inside some_notebook.ipynb I simply want to get the methods from some_processing.py. Now we I run

from src.preprocess import some_processing

from some_notebook.ipynb it always throws

ModuleNotFoundError: No module named 'src'

I found multiple questions regarding this and played around with sys.path.append(<path-to-src>). But I couldn’t solve it. Which path do I provide? Something like ../src didnt work?

I checked for example the AlphaFold project from DeepMind and they are using it also with this structure. I tried to replicate exactly like they did.

How can I solve this? Which path do I provide in sys.path.append()?

I appreciate any help!

Asked By: PeeteKeesel

||

Answers:

I found the answer. Running

sys.path.insert(1, os.path.join(sys.path[0], '../src'))

made it possible to import anything from parent module src.

Answered By: PeeteKeesel

Adding path to your src/ directory into the sys.path is OK, but import should be like this: from preprocess import some_processing.

Off-topic 1: I’m using the same project structure. So this is usually the first cell of my average jupyter notebook:

%reload_ext autoreload
%autoreload 2

import sys
import os.path as osp

SRC_SUBDIR = '../src/'
SRC_SUBDIR = osp.abspath(SRC_SUBDIR)
if SRC_SUBDIR not in sys.path:
    print(f'Adding source directory to the sys.path: {SRC_SUBDIR!r}')
    sys.path.insert(1, SRC_SUBDIR)

Off-topic 2: In this case, as src is not needed in import statement, you don’t need the file src/__init__.py.

Answered By: dmitmatveev
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.