0

I'm working on a python project in JupyterLab and I'm confused about the package and module import. I get a ModuleNotFoundError (see below) and I don't understand why.

This is my abstracted folder structure:

top/
|
|– sub
|   |– __init__.py
|   |– pre.py
|   |– post.py
|   |– sub_main.ipynb
|
|– top_main.ipynb

The __init__.py is empty.

These are my imports:

post.py

from pre import xyz

sub_main.ipynb

import pre, post

top_main.ipynb

from sub import pre
from sub import post

ModuleNotFoundError: No module named pre

So, from sub import pre is working, but from sub import post is not.
Does anyone know what I'm doing wrong?

Thanks!
carlo

CarloP
  • 99
  • 1
  • 12
  • Have you tried restarting the kernel? – krassowski Sep 03 '21 at 09:25
  • @carloP i suggest you to check this issue that looks similar https://stackoverflow.com/questions/43120112/module-not-found-during-import-in-jupyter-notebook – Carlo Sep 03 '21 at 10:27
  • @krassowski yes I restarted the kernel multiple times, it doesn't help. – CarloP Sep 03 '21 at 11:22
  • 1
    You need to perform a relative import in `post.py` since you are importing from the same-level module in a package; try `from .pre import xyz`. – krassowski Sep 03 '21 at 12:25
  • Does this answer your question? [ModuleNotFound: No module named 'module\_name'](https://stackoverflow.com/questions/56462283/modulenotfound-no-module-named-module-name) – krassowski Sep 03 '21 at 12:25
  • @krassowski yes this answers my question! Sorry for the late reply, and thanks very much for your support! – CarloP Nov 15 '21 at 08:06

0 Answers0