title: Pickle a dictionary of class instances in Python
body: <p><strong>How can I <code>pickle</code> a dictionary object that contains instances of a Class in one file (Python File 1) and <code>pickle.load</code> in another file (Python File 2)?</strong> </p>
  
  <p>I have a HUGE complicated dataset made up of several files and I created a class to store all of my attributes.  I made a dictionary to store all of the samples and attributes . key = sample, value = instance of the class containing the atributes.  Example below:</p>
  
  <pre><code>#Python File 1
  import random
  
  class Storage:
      def __init__(self,label,x,y): 
          self.label = label; self.x = x; self.y = y
      def get_x(self): return(self.x)
      def get_y(self): return(self.y)
  
  D_var_instance = {}
  L = ["A","B","C"]
  
  for var in L: 
      D_var_instance[var] = Storage(label=var,x=random.random(),y=random.random())
  
  print(D_var_instance["A"])
  #&lt;__main__.Storage instance at 0x102811128&gt;
  
  print(D_var_instance["A"].get_x())
  #0.193517721574
  </code></pre>
  
  <p>It takes me a long time to make this with my real dataset, I tried using <code>pickle</code> and <code>pickle.dump</code> the dictionary object but it's not working:</p>
  
  <pre><code>#Python File 1
  import pickle
  pickle.dump(D_var_instance,open("/path/to/dump.txt","w"))
  pickle.dump(Storage, open("/path/to/storagedump.txt","w"))
  </code></pre>
  
  <p>I tried loading in another Python file with this code:</p>
  
  <pre><code>#Python File 2
  import pickle
  Storage = pickle.load(open("/path/to/storagedump.txt","r"))
  D_var_instance = pickle.load(open("/path/to/dump.txt","r"))
  </code></pre>
  
  <p>Got this error:</p>
  
  <pre><code>AttributeError: 'module' object has no attribute 'Storage'
  </code></pre>