3

I am using Jupyter Notebook and trying to use some classes from a enums.py file. If I add new classes to that file, then it seems that I have to always restart the kernel in order to use them in other `.ipynb' files, which is not the best solution.

After searching more, I've found a better solution here How to reload a module's function in Python?, but I'm getting the following error and I'm not sure how to solve that:

ModuleNotFoundError: spec not found for the module 'enums'

This is my code from notebook.ipynb:

from importlib import reload
reload(enums)
from enums import FieldTypes, MyEnum

And this is the folder structure:

  Jupyter_Notebooks
  |--helpers
  |  |-- __init__.py
  |  |-- enums.py
  |  |-- notebook.ipynb
Valip
  • 4,440
  • 19
  • 79
  • 150

1 Answers1

0

From the documentation (help(reload)): "The module must have been successfully imported before."

You must import enums for reload(enums) to work. See here for details on from-imports.

Niklas Mertsch
  • 1,399
  • 12
  • 24
  • I have added `import enums` before `from importlib import reload`, but it still throws the same error – Valip Oct 14 '20 at 10:02
  • But `import enums` works and `import enums; from importlib import reload; reload(enums)` fails at `reload(enums)`? Does it work in a `.py` file executed via `python` directly? – Niklas Mertsch Oct 14 '20 at 10:37
  • Well...I've restarted the kernel and now it's not finding the `enums` module anymore: `ModuleNotFoundError: No module named 'enums'`. I might give up on Jupyter Notebook since you have to do a lot of workarounds in order to get the code organised in modules – Valip Oct 14 '20 at 10:43