从诗歌项目内的 jupyter 笔记本调用模块会导致“无名为模块”错误

问题描述 投票:0回答:1

我正在开发一个带有诗歌的模块,其目录结构如下

./
   /my_module
      (module files)
   /tests
      test_thing.py
   /jupyter
      demo.ipynb

在测试文件和jupyter笔记本中,有如下导入代码

from my_module import thingy, other_thingy

现在从项目根目录

poetry run pytest
可以正常工作,但是通过
poetry run jupyter lab
调用jupyter lab,然后运行笔记本会导致
No module named my_module
错误。为什么模块在 pytest 的路径上而不是 jupyter 的路径上?

当然我可以执行以下操作,但是诗歌环境中的其他模块都不可用。我该如何解决这个问题?

import sys
sys.path.append('../)
python pytest jupyter python-poetry
1个回答
0
投票

解决了。当项目中未使用诗歌安装 jupyter 时,就会出现此问题。只需

poetry add jupyter
(对于任何组),然后
poetry run jupyter lab
即可给出预期结果。否则,它会默默地退回到系统安装的 jupyter lab 版本。

© www.soinside.com 2019 - 2024. All rights reserved.