gpt4 book ai didi

python - 在 Jupyter Notebook 中导入模块 - 路径

转载 作者:太空狗 更新时间:2023-10-30 02:53:30 25 4
gpt4 key购买 nike

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

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

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

我试过:

from ..src.configuration import Config

但这给了我一个 ValueError:

ValueError: attempted relative import beyond top-level package

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

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

最佳答案

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

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

from src.configuration import Config

关于python - 在 Jupyter Notebook 中导入模块 - 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49282122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com