Python dill: Pickle namedtuple doesnt seem to work
<p>I have namedtuple inside a class. When pickling using dill, it complains the classic issue of not being able to find the namedtuple object at top module. </p>

<pre><code>import dill as pickle    
class NNRTMParse(object):

    def __init__(self,logfile)):
        .
        .
        .
        .

        self.TM = namedtuple('TM',tmeas_fields)
        #print self.TM
        CFH = namedtuple('CFH',cfhdr_fields)
        PM = namedtuple('PM',pmeas_fields2)
        print PM
</code></pre>

<p>This is default assignment for self.TM and others, and I dynamically assign namedtuple as I parse the log.</p>

<p>But pickling :</p>

<pre><code> if __name__ == "__main__":

    filename = 'dbggen_rx_loc_2-llh_rtm_lla_out_20160929_130711_day2_4381_JN2_SN64_rtmproc_2M5M.txt'
    N = NNRTMParse(filename)
    N.parse()
    N.get_rx_loc('oak484_bora-llh')

    filehandler = open("NNRTMParse_JB2-SN052.obj","wb")
    pickle.dump(N,filehandler)
    filehandler.close()






Traceback (most recent call last):
  File "C:/NN_Hardware/spos_proc/NNRTMParse.py", line 937, in &lt;module&gt;
    pickle.dump(N,filehandler)
  File "C:\Python27\lib\site-packages\dill\dill.py", line 236, in dump
    pik.dump(obj)
  File "C:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "C:\Python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:\Python27\lib\pickle.py", line 419, in save_reduce
    save(state)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 681, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\site-packages\dill\dill.py", line 1189, in save_type
    StockPickler.save_global(pickler, obj)
  File "C:\Python27\lib\pickle.py", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle &lt;class '__main__.TM'&gt;: it's not found as __main__.TM
</code></pre>

<p>I am not sure if there is any way out. Else would have to route thru the path just to nit-pick the dataframes to pickle, which I hate to do.</p>

<p>Any help is very appreciated.</p>

<p>The issue is the <code>self.TM</code> <code>namedtuple</code>. If you don't use the <code>namedtuple</code> as a class attribute, then your class should pickle.</p>

<pre><code># file: xxx.py
from collections import namedtuple

class NNRTMParse(object):

  def __init__(self):
    TM = namedtuple("TM", 'a')
    CFH = namedtuple("CFH", 'b')
    print CFH
</code></pre>

<p>It should work like this:</p>

<pre><code>&gt;&gt;&gt; from xxx import *
&gt;&gt;&gt; a = NNRTMParse()
&lt;class 'xxx.CFH'&gt;
&gt;&gt;&gt; import dill
&gt;&gt;&gt; dill.copy(a)
&lt;xxx.NNRTMParse object at 0x107da3350&gt;
</code></pre>

<p>However, as soon as you try to use the <code>namedtuple</code> as a attribute, it fails with the error you are seeing.</p>

<p>I'd suggest adding a <code>__reduce__</code> method (<a href="https://docs.python.org/3/library/pickle.html#object.__reduce__" rel="nofollow">https://docs.python.org/3/library/pickle.html#object.<strong>reduce</strong></a>) to tell <code>pickle</code> how to serialize the state of the class instance.</p>

python pickle namedtuple dill