1

I'm really new at this and struggling some. I have written a class that has several methods that do some basic calculations. I've written it in jupyter notebook, and save it as a BaseModel.ipynb file. the code all works when i use it in the same notebook.

i want to be able to import the class when working in a fresh jupyter notebook to keep things less cluttered as the class is fairly long, and thought i would be able to save it as an .ipynb or .py and then in new notebook do import BaseModel but then i get the error

ModuleNotFoundError Traceback (most recent call last) in ----> 1 import BaseModel

ModuleNotFoundError: No module named 'BaseModel'

I've tried to search here and see responses about adding an empty notebook called 'init.py but doesn't work. I've watched videos but can't get it to do anything.

Thanks in advance, and struggling here! I've done a bunch of courses that show you how to do basic data analysis in notebooks and its great, but finding learning about classes and how they interact with notebooks and import is losing me, any resources/courses to view would be appreicated too.

Thanks very much

AMC
  • 2,642
  • 7
  • 13
  • 35
dannytrees
  • 11
  • 3
  • 1
    iPython notebooks are not python modules. `import` won't work with them. You have to save your class in a python `.py` file. You might also need to add a `__init__.py` file to make the import work – rdas Apr 17 '20 at 21:37
  • thanks for the feedback - where does one add the __init__.py file? In the same localhost directory where all the notebooks get saved? i have tried that, i have a blank notebook, save it as __init__.py and then download as .py, save it in same directory but still get same error? – dannytrees Apr 17 '20 at 21:40
  • would i just import it straight from the first cell in new notebook 'import BaseModel' and thats it? – dannytrees Apr 17 '20 at 21:41
  • Does this answer your question? [ipynb import another ipynb file](https://stackoverflow.com/questions/20186344/ipynb-import-another-ipynb-file) – AMC Apr 17 '20 at 22:00

1 Answers1

0

There are some tricks you can try here.

Import functions/classes defined in Jupyter Notebook

guoyili
  • 83
  • 1
  • 5