0

I have a class that I want to share between different files or computers? I can pickle and load it from the same jupyter notebook. However, I cannot load it from a different machine/or different notebook. I have tried the following,

Initialize and and save from a jupyter notebook

# Simplified class definition
class MyClass:
    def __init__(self, name):
        self.name = name
        self.dataToIndex = {}
        self.index = 0
    
    def addData(self, dataReceived):
        self.dataToIndex[dataReceived] = self.index
        self.index += 1

# initialize
my_dataset = MyClass("My_test_dataset")

# add some data
my_dataset.addData("One")
my_dataset.addData("Two")

# check data
my_dataset.dataToIndex


# save from one juputer notebook
import pickle

with open("path.obj", "wb") as inp:
    pickle.dump(my_dataset, inp, pickle.HIGHEST_PROTOCOL)
    

# Read from the same jupyter notebook
with open("path.obj", 'rb') as inp:
    transferred = pickle.load(inp)

# Output looks good
transferred.dataToIndex
{'One': 0, 'Two': 1}

Read from the new jupyter notebook

import pickle
with open("path.obj", 'rb') as inp:
    transferred = pickle.load(inp)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-44a1a18ebb1b> in <module>
      2 # Read from the same jupyter notebook
      3 with open("path.obj", 'rb') as inp:
----> 4     transferred = pickle.load(inp)

AttributeError: Can't get attribute 'MyClass' on <module '__main__'>

Now, I want to be able to load into a different python script on in a different jupyter notebook.

I have checked this, Saving an Object (Data persistence) and https://www.stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.html

But could not figure out a solution. Any help is appreciated.

Droid-Bird
  • 1,417
  • 5
  • 19
  • 43

1 Answers1

0

There is a tons of helpful posts on this. However, most of those fall under, how to save from within the class etc. For a future beginner like me, I just want to provide a simple solution that worked for me.

Take the class out of the notebook and put in a script like, my_class_def.py

class MyClass:
    def __init__(self, name):
        self.name = name
        self.dataToIndex = {}
        self.index = 0
    
    def addData(self, dataReceived):
        self.dataToIndex[dataReceived] = self.index
        self.index += 1

Then use the pickle to save as it is.

In the different(new) script, import the class first using,

from my_class_def import MyClass

and then load the picked file.

Droid-Bird
  • 1,417
  • 5
  • 19
  • 43