title: Python3 Cannot pickle self-defined objects
body: <p>After moving a project to Python3 from Python2, I am no longer able to pickle my own objects. To restate, in python 2.7.2 I had no problems, no I get </p>
  
  <pre><code>_pickle.PicklingError: Can't pickle &lt;class 'mtgt.MTGTree'&gt;: import of module 'mtgt' failed
  </code></pre>
  
  <p>I've done some searching on the web and it sounds like it has to do with the way I am importing my objects. I am using Python 3.5.2/GCC 5.4.0 20160609 on Linux. For background, I have a project with the following structure</p>
  
  <pre><code>* Lituus
    - \_\_init\_\_.py
    - lituus
      + \_\_init\_\_.py
      + mtg.py
      + multiverse.py
      + mtgcard.py
      + mtgl
         * \_\_init\_\_.py
         * mtgl.py
         * tagger.py
         * lexer.py
         * parser.py
         * grapher.py
         * mtgt.py
         * list_util.py 
  </code></pre>
  
  <p>and I have a lituus.pth file in /usr/lib/python3/dist-packages so I can run python3 from a command prompt in any directory and import my projects files</p>
  
  <p>I want to pickle MTGTree objects which are defined in mtgt.py. Now if I run python3 from the directory containing mtgt and <code>import mtgt</code> I an dump the object</p>
  
  <pre><code>&gt;&gt;&gt; import mtgt
  &gt;&gt;&gt; import pickle
  &gt;&gt;&gt; fout = open('test.pkl','wb')
  &gt;&gt;&gt; pickle.dump(t,fout)
  &gt;&gt;&gt; fout.close()
  &gt;&gt;&gt; exit()
  </code></pre>
  
  <p>and load the object</p>
  
  <pre><code>&gt;&gt;&gt; import mtgt
  &gt;&gt;&gt; import pickle
  &gt;&gt;&gt; fin = open('test.pkl','rb')
  &gt;&gt;&gt; t = pickle.load(fin)
  &gt;&gt;&gt; t.root
  'root'
  &gt;&gt;&gt; fin.close()
  &gt;&gt;&gt; exit()
  </code></pre>
  
  <p>and, I can even run python3 from anywhere and load the pickled tree </p>
  
  <pre><code>&gt;&gt;&gt; import lituus.mtgl.mtgt as mtgt
  &gt;&gt;&gt; import pickle
  &gt;&gt;&gt; fin = open('test.pkl','rb')
  &gt;&gt;&gt; t=pickle.load(fin)
  &gt;&gt;&gt; fin.close()
  &gt;&gt;&gt; t.root
  'root'
  &gt;&gt;&gt; exit()
  </code></pre>
  
  <p>The problem occurs when I try to dump a tree using <code>import.mtgl.mtgt as mtgt</code></p>
  
  <pre><code>&gt;&gt;&gt; import lituus.mtgl.mtgt as mtgt
  &gt;&gt;&gt; import pickle
  &gt;&gt;&gt; t = mtgt.MTGTree()
  &gt;&gt;&gt; t.root
  'root'
  &gt;&gt;&gt; fout = open('test.pkl','wb')
  &gt;&gt;&gt; pickle.dump(t,fout)
  </code></pre>
  
  <p>This leads to the aforementationed 
       _pickle.PicklingError: Can't pickle : import of module 'mtgt' failed    </p>
  
  <p>So, obviously I'm doing something wrong here but WTF? This works in Python 2.7 with no issues and there is no problem using pickle to load a pickled file. </p>
  
  <p>I cannot find anything concrete and was hoping one of yall had some insights. Thanks.</p>