Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 309
RaspyVotan Avatar asked Jan 28 '23 12:01

RaspyVotan


1 Answers

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
like image 72
Oppy Avatar answered Feb 07 '23 18:02

Oppy