title: How to unpickle an object whose class exists in a different namespace (python)?
body: <p>If I have a script that defines a class:</p>
  
  <pre><code>script = """
  
  class myClass:
      def __init__(self):
          self.name = 'apple'
          self.color = 'green'
  
  """
  </code></pre>
  
  <p>and then exec this script in its own namespace dict:</p>
  
  <pre><code>NS = {}
  exec script in NS
  </code></pre>
  
  <p>and then create an instance of the class and pickle it:</p>
  
  <pre><code>a = NS['myClass']()
  
  import pickle
  
  save = pickle.dumps(a)
  </code></pre>
  
  <p>Now if I try to unpickle it:</p>
  
  <pre><code>load = pickle.loads(save)
  </code></pre>
  
  <p>I get the error</p>
  
  <pre><code>AttributeError: 'module' object has no attribute 'myClass'
  </code></pre>
  
  <p>I gather that this doesn't work because python doesn't know where to find myClass in order to rebuild the object.  But myClass does exist in the NS dict.  Is there a way to tell pickle where to find the class for the object it is loading?</p>