RaspyVotan
RaspyVotan

Reputation: 131

Import modules in Jupyter Notebook - Path

I have the following structure in my python project:

BASE_DIRECTORY
¦- data
¦- notebooks
¦  \
¦   ***here are the jupyter notebooks***
¦   __init__.py
¦   analysis.ipynb
¦
¦- src
¦  \
¦   ***here are further modules***
¦   __init__.py
¦   configuration.py
¦

I would like to import class Config() from configuration.py into a jupyter notebook analysis.ipynb.

I tried:

from ..src.configuration import Config

but that gives me a ValueError:

ValueError: attempted relative import beyond top-level package

Can someone direct how to achieve this? I would prefer to work with relative paths rather than with changing PATH variables.

I feel there are some specifics about Jupyter that I am not aware of, e.g. it seems to be harder to refer to the current path.

Upvotes: 7

Views: 6059

Answers (1)

Oppy
Oppy

Reputation: 2897

I saw that you said that you do not want to alter the path variables, but using sys to do this gives a solution:

import sys
sys.path.append('..')

from src.configuration import Config

Upvotes: 10

Related Questions