在Jupyter Notebook中导入模块-路径

Ras*_*tan 4 python import jupyter jupyter-notebook

我的python项目中具有以下结构:

BASE_DIRECTORY
¦- data
¦- notebooks
¦  \
¦   ***here are the jupyter notebooks***
¦   __init__.py
¦   analysis.ipynb
¦
¦- src
¦  \
¦   ***here are further modules***
¦   __init__.py
¦   configuration.py
¦
Run Code Online (Sandbox Code Playgroud)

我想将类Config()从configuration.py导入到jupyter笔记本analysis.ipynb中。

我试过了:

from ..src.configuration import Config
Run Code Online (Sandbox Code Playgroud)

但这给了我一个ValueError:

ValueError: attempted relative import beyond top-level package
Run Code Online (Sandbox Code Playgroud)

有人可以指导如何实现这一目标吗?我宁愿使用相对路径而不是更改PATH变量。

我觉得我不了解Jupyter的一些细节,例如,似乎很难参考当前路径。

Opp*_*ppy 5

我看到您说您不想更改路径变量,但是使用sys可以解决此问题:

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

from src.configuration import Config
Run Code Online (Sandbox Code Playgroud)