Search code examples
python-3.xjupyter-notebookjupyterjupyter-labjupyterhub

jupyter lab: opening a ipynb file from another ipynb file using %run, same directory already


In jupyter notebook, I am trying to open a ipynb file create.ipynb in which I made a class called Animals, from another ipynb file Coursera_test.ipynb. I have already checked, they are in the same directory. They are literally just in my desktop. create.ipynb contains the following code

class Animals:
    def __init__(self, animal, age, colour):
        self.animaled= animal
        self.ageed= age
        self.coloured= colour
        self.cuteness= "the cutest"
    
    def move(self):
        print(f"The {self.animaled} is moving")

    def stop (self):
        print (f"The {self.animaled} has stopped")

Coursera_test.ipynb contains the following code

%run "create.ipynb"
from create import Animals

When I run the lines one by one in the Coursera_test.ipynb,the first line with the run command has no error, but the second throws out the following error message

----> 1 from create import Animals

ModuleNotFoundError: No module named 'create'

I also tried

%run /Users/myname/Desktop/create.ipynb
from create import Animals

and got the same error message. I already checked for typos etc. The strange thing is that when I export the create.ipynb file into a python file create.py, and run the correct code for opening such python files, the code runs fine with no error message i.e. I can indeed import Animals from create.

Can anyone help me with this? I already checked the other answers on stack overflow but no one else seems to have exactly the same problem.


Solution

  • It seems the main goal you are trying to accomplish is to actually import the class (Animals) contained within the create.ipynb notebook, and not necessarily the notebook itself.

    Proposed Solution

    EDIT: Following @Wayne's comment(s): The Animals class can be used directly once %run create.ipynb is executed first. The availability can be queried using dir().


    The initial solution was to have %run create.ipynb import Animals as the first line but the appended import statement is redundant (although, that was the technique suggested here).

    The import statement in Coursera_test.ipynb should look like this:

    # Works, but is redundant
    # %run create.ipynb import Animals
    
    %run create.ipynb
    
    # Animals class is immediately available for use
    dog = Animals("Dalmatian", "3", "Black and White")
    
    # Fancy tabulation
    properties = vars(dog)
    pads = [len(str(max(properties.keys()))), len(str(max(properties.values())))]
    row0 = "| %-9s ||" % "Property"
    row1 = "| %-9s ||" % "Value"
    for k,v in zip(properties.keys(), properties.values()):
        pad = max(max(pads), len(k), len(v))
        top = " %-*s |" % (pad, k)
        bottom = " %-*s |" % (pad, v)
        row0 += top
        row1 += bottom
    print(row0); print("-"*len(row0));print(row1)
    

    Output:

    Animals class successfully imported from notebook