在Jupyter Notebook - Path中导入模块

时间:2018-03-14 15:45:15

标签: python import jupyter-notebook jupyter

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

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

我想从configuration.py导入类Config()到jupyter notebook analysis.ipynb。

我试过了:

from ..src.configuration import Config

但是这给了我一个ValueError:

ValueError: attempted relative import beyond top-level package

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

我觉得有一些关于Jupyter的细节我不知道,例如它似乎更难以引用当前的路径。

1 个答案:

答案 0 :(得分:3)

我看到你说你不想改变路径变量,但使用sys来做这个就给出了一个解决方案:

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

from src.configuration import Config
相关问题