0

I am trying to import a class i created in one Jupyter notebook, to a different notebook and i get an error message saying the following:

from newclassattempt import MyClass
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-4337d8f762d7> in <module>
----> 1 from newclassattempt import MyClass

ModuleNotFoundError: No module named 'newclassattempt'

How can i get around this and import my class?

2 Answers2

0

Please note that for the import to work we need the module to be a Python File, meaning with the extension .py.

The Jupyter Notebooks are not your plain python files and are thus not imported by normal python machinery

You can actually import a Jupyter Notebook using Hooks. A lot of it is detailed in the documentation. But I also found another method

In the thisnotebook.ipynb (the one you are working on), all you need to do is use these commands:

import import_ipynb
import newclassattempt

For this to work ensure that you install:

pip install import-ipynb

You can also use your from newclassattempt import MyClass

(The above was taken from this)

CagesThrottleUs
  • 372
  • 1
  • 2
  • 9
  • thank you, the file imported, but still saying unable to import my class - perhaps i made an error with my class formation. will look into that – Xamishka May 17 '20 at 12:00
0

Update: I found a fully functional solution.

As mentioned in other comments, the class needs to be stored as a .py file in the same folder as the jupyter notebook. the following code then worked:

import newclassattempt
ob = newclassattempt.MyClass(1)

where newclassattempt is the name of the .py file, ob is the object created and MyClass is the class in the .py file. The reason my class couln't be imported is because I had not referenced the original file name