简体   繁体   中英

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, eg it seems to be harder to refer to the current path.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM