title: unittest unable to import class from pickle (AttributeError: Can't get attribute...)
body: <p>I need a unittest to load a previously saved class in a pickle. However, when I load the pickle in the unittest (out of unittest works), it raises the error:</p>
  <blockquote>
  <p>AttributeError: Can't get attribute 'Foo' on &lt;module
  'unittest.<strong>main</strong>' from '...\unittest\<strong>main</strong>.py'&gt;</p>
  </blockquote>
  <p>Code example to save the class (I save this code in <code>run_and_save_class.py</code>):</p>
  <pre><code>from pickle import dump
  from pickle import load
  from pickle import HIGHEST_PROTOCOL
  
  class Foo(object):
      
      def __init__(self):
          self.bar = None
          self.file_out = &quot;./out.pkl&quot;
  
      def save_class(self):
          with open(self.file_out, &quot;wb&quot;) as file_out:
              dump(self, file_out, protocol=HIGHEST_PROTOCOL)
              
      def load_class(self):
          with open(self.file_out, &quot;rb&quot;) as file_out:
              cls = load(file_out)
          return cls
  
  if __name__ == &quot;__main__&quot;:
      cls = Foo()
      cls.bar = &quot;saving a bar&quot;
      cls.save_class()
  </code></pre>
  <p>Code to test the class (I save this code in <code>unittest_class.py</code>):</p>
  <pre><code>import unittest
  from run_and_save_class import Foo
  
  
  class ClassValidation(unittest.TestCase):
      
      def __init__(self, *args, **kwargs):
          print(&quot;init&quot;)
          self.cls = Foo
          self.instance = Foo().load_class()
          print(&quot;class loaded&quot;)
          unittest.TestCase.__init__(self, *args, **kwargs)
      
      def test_anything(self):
          pass
  </code></pre>
  <p>I run in Anaconda Prompt:</p>
  <pre><code>python run_and_save_class.py
  
  python -m unittest -v unittest_class.py
  </code></pre>
  <p>The latter is the one that raises the error.</p>
  <p>However, this works in a notebook.</p>
  <pre><code>from run_and_save_class import Foo
  cls = Foo().load_class()
  </code></pre>
  <p>I don't understand why it doesn't in a unittest.</p>