Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to dump_session after get_ipython #255

Open
dice89 opened this issue Feb 12, 2018 · 12 comments · May be fixed by #475
Open

Failure to dump_session after get_ipython #255

dice89 opened this issue Feb 12, 2018 · 12 comments · May be fixed by #475

Comments

@dice89
Copy link

dice89 commented Feb 12, 2018

Session to dump

 from IPython import get_ipython
    ipython = get_ipython()
    ipython.magic("matplotlib inline")

ERROR:

TypeError                                 Traceback (most recent call last)
<ipython-input-2-ec1dcb4604c0> in <module>()
      1 import dill
----> 2 dill.dump_session(filename="/tmp/pynb-cache-570030c2-session.dill")

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in dump_session(filename, main, byref)
    370         pickler._recurse = False # disable pickling recursion for globals
    371         pickler._session = True  # is best indicator of when pickling a session
--> 372         pickler.dump(main)
    373     finally:
    374         f.close()

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in dump(self, obj)
    410         if self.proto >= 4:
    411             self.framer.start_framing()
--> 412         self.save(obj)
    413         self.write(STOP)
    414         self.framer.end_framing()

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    477         f = self.dispatch.get(t)
    478         if f is not None:
--> 479             f(self, obj) # Call unbound method with explicit self
    480             return
    481 

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in save_module(pickler, obj)
   1236                 + ["__builtins__", "__loader__"]]
   1237             pickler.save_reduce(_import_module, (obj.__name__,), obj=obj,
-> 1238                                 state=_main_dict)
   1239             log.info("# M1")
   1240         else:

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    625 
    626         if state is not None:
--> 627             save(state)
    628             write(BUILD)
    629 

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    477         f = self.dispatch.get(t)
    478         if f is not None:
--> 479             f(self, obj) # Call unbound method with explicit self
    480             return
    481 

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_dict(self, obj)
    812 
    813         self.memoize(obj)
--> 814         self._batch_setitems(obj.items())
    815 
    816     dispatch[dict] = save_dict

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in _batch_setitems(self, items)
    838                 for k, v in tmp:
    839                     save(k)
--> 840                     save(v)
    841                 write(SETITEMS)
    842             elif n:

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    522 
    523         # Save the reduce() output and finally memoize the object
--> 524         self.save_reduce(obj=obj, *rv)
    525 
    526     def persistent_id(self, obj):

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    625 
    626         if state is not None:
--> 627             save(state)
    628             write(BUILD)
    629 

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    477         f = self.dispatch.get(t)
    478         if f is not None:
--> 479             f(self, obj) # Call unbound method with explicit self
    480             return
    481 

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_dict(self, obj)
    812 
    813         self.memoize(obj)
--> 814         self._batch_setitems(obj.items())
    815 
    816     dispatch[dict] = save_dict

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in _batch_setitems(self, items)
    838                 for k, v in tmp:
    839                     save(k)
--> 840                     save(v)
    841                 write(SETITEMS)
    842             elif n:

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    522 
    523         # Save the reduce() output and finally memoize the object
--> 524         self.save_reduce(obj=obj, *rv)
    525 
    526     def persistent_id(self, obj):

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    625 
    626         if state is not None:
--> 627             save(state)
    628             write(BUILD)
    629 

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    477         f = self.dispatch.get(t)
    478         if f is not None:
--> 479             f(self, obj) # Call unbound method with explicit self
    480             return
    481 

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_dict(self, obj)
    812 
    813         self.memoize(obj)
--> 814         self._batch_setitems(obj.items())
    815 
    816     dispatch[dict] = save_dict

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in _batch_setitems(self, items)
    838                 for k, v in tmp:
    839                     save(k)
--> 840                     save(v)
    841                 write(SETITEMS)
    842             elif n:

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    477         f = self.dispatch.get(t)
    478         if f is not None:
--> 479             f(self, obj) # Call unbound method with explicit self
    480             return
    481 

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_dict(self, obj)
    812 
    813         self.memoize(obj)
--> 814         self._batch_setitems(obj.items())
    815 
    816     dispatch[dict] = save_dict

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in _batch_setitems(self, items)
    838                 for k, v in tmp:
    839                     save(k)
--> 840                     save(v)
    841                 write(SETITEMS)
    842             elif n:

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    522 
    523         # Save the reduce() output and finally memoize the object
--> 524         self.save_reduce(obj=obj, *rv)
    525 
    526     def persistent_id(self, obj):

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    625 
    626         if state is not None:
--> 627             save(state)
    628             write(BUILD)
    629 

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    477         f = self.dispatch.get(t)
    478         if f is not None:
--> 479             f(self, obj) # Call unbound method with explicit self
    480             return
    481 

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_dict(self, obj)
    812 
    813         self.memoize(obj)
--> 814         self._batch_setitems(obj.items())
    815 
    816     dispatch[dict] = save_dict

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in _batch_setitems(self, items)
    838                 for k, v in tmp:
    839                     save(k)
--> 840                     save(v)
    841                 write(SETITEMS)
    842             elif n:

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    477         f = self.dispatch.get(t)
    478         if f is not None:
--> 479             f(self, obj) # Call unbound method with explicit self
    480             return
    481 

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_dict(self, obj)
    812 
    813         self.memoize(obj)
--> 814         self._batch_setitems(obj.items())
    815 
    816     dispatch[dict] = save_dict

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in _batch_setitems(self, items)
    838                 for k, v in tmp:
    839                     save(k)
--> 840                     save(v)
    841                 write(SETITEMS)
    842             elif n:

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    522 
    523         # Save the reduce() output and finally memoize the object
--> 524         self.save_reduce(obj=obj, *rv)
    525 
    526     def persistent_id(self, obj):

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    625 
    626         if state is not None:
--> 627             save(state)
    628             write(BUILD)
    629 

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    477         f = self.dispatch.get(t)
    478         if f is not None:
--> 479             f(self, obj) # Call unbound method with explicit self
    480             return
    481 

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save_dict(self, obj)
    812 
    813         self.memoize(obj)
--> 814         self._batch_setitems(obj.items())
    815 
    816     dispatch[dict] = save_dict

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in _batch_setitems(self, items)
    838                 for k, v in tmp:
    839                     save(k)
--> 840                     save(v)
    841                 write(SETITEMS)
    842             elif n:

~/.pyenv/versions/3.4.3/lib/python3.4/pickle.py in save(self, obj, save_persistent_id)
    497             reduce = getattr(obj, "__reduce_ex__", None)
    498             if reduce is not None:
--> 499                 rv = reduce(self.proto)
    500             else:
    501                 reduce = getattr(obj, "__reduce__", None)

~/.pyenv/versions/pynbexperiments/lib/python3.4/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.socket.Socket.__reduce_cython__()

TypeError: no default __reduce__ due to non-trivial __cinit__
TypeError: no default __reduce__ due to non-trivial __cinit__
@mmckerns
Copy link
Member

@dice89: I see from your traceback that you are using python 3.4. However, I'm not seeing the same traceback as you. Can you edit your code above so that I can attempt to exactly reproduce the error (i.e. I don't see you dill.dump_session above)?

Regardless, I do see an error whenever I try to dump a session that has similar code with an instance of IPython, so that gives me something to work from.

@mmckerns mmckerns changed the title Dill Serialisation Fails when I load ipython Failure to dump_session after get_ipython Mar 18, 2018
@CharryWu
Copy link

I got the same error output when i tried to run some very simple test code in a new notebook.

Code

import dill
a = 3
y = 6
dill.dump_session('test.db')

Traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-775267c578f1> in <module>()
      1 a = 3
      2 y = 6
----> 3 dill.dump_session(SESSION_FILE_NAME)

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in dump_session(filename, main, byref)
    370         pickler._recurse = False # disable pickling recursion for globals
    371         pickler._session = True  # is best indicator of when pickling a session
--> 372         pickler.dump(main)
    373     finally:
    374         f.close()

~/anaconda3/lib/python3.6/pickle.py in dump(self, obj)
    407         if self.proto >= 4:
    408             self.framer.start_framing()
--> 409         self.save(obj)
    410         self.write(STOP)
    411         self.framer.end_framing()

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module(pickler, obj)
   1236                 + ["__builtins__", "__loader__"]]
   1237             pickler.save_reduce(_import_module, (obj.__name__,), obj=obj,
-> 1238                                 state=_main_dict)
   1239             log.info("# M1")
   1240         else:

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/pickle.py in save_list(self, obj)
    779 
    780         self.memoize(obj)
--> 781         self._batch_appends(obj)
    782 
    783     dispatch[list] = save_list

~/anaconda3/lib/python3.6/pickle.py in _batch_appends(self, items)
    806                 write(APPENDS)
    807             elif n:
--> 808                 save(tmp[0])
    809                 write(APPEND)
    810             # else tmp is empty, and we're done

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    850                 k, v = tmp[0]
    851                 save(k)
--> 852                 save(v)
    853                 write(SETITEM)
    854             # else tmp is empty, and we're done

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    519 
    520         # Save the reduce() output and finally memoize the object
--> 521         self.save_reduce(obj=obj, *rv)
    522 
    523     def persistent_id(self, obj):

~/anaconda3/lib/python3.6/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    632 
    633         if state is not None:
--> 634             save(state)
    635             write(BUILD)
    636 

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    474         f = self.dispatch.get(t)
    475         if f is not None:
--> 476             f(self, obj) # Call unbound method with explicit self
    477             return
    478 

~/anaconda3/lib/python3.6/site-packages/dill/dill.py in save_module_dict(pickler, obj)
    869             # we only care about session the first pass thru
    870             pickler._session = False
--> 871         StockPickler.save_dict(pickler, obj)
    872         log.info("# D2")
    873     return

~/anaconda3/lib/python3.6/pickle.py in save_dict(self, obj)
    819 
    820         self.memoize(obj)
--> 821         self._batch_setitems(obj.items())
    822 
    823     dispatch[dict] = save_dict

~/anaconda3/lib/python3.6/pickle.py in _batch_setitems(self, items)
    845                 for k, v in tmp:
    846                     save(k)
--> 847                     save(v)
    848                 write(SETITEMS)
    849             elif n:

~/anaconda3/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
    494             reduce = getattr(obj, "__reduce_ex__", None)
    495             if reduce is not None:
--> 496                 rv = reduce(self.proto)
    497             else:
    498                 reduce = getattr(obj, "__reduce__", None)

~/anaconda3/lib/python3.6/site-packages/zmq/backend/cython/socket.cpython-36m-darwin.so in zmq.backend.cython.socket.Socket.__reduce_cython__()

TypeError: no default __reduce__ due to non-trivial __cinit__

Test Environment

Jupyter Notebook: 5.4
Python: 3.6 (installed via anaconda)
OS: macOS 10.13.4 high sierra

@weigetang406team
Copy link

HELLO,I have the same error above when i run code in jupyter~but it works when i use pycharm.looking forward to your answers

@mmckerns
Copy link
Member

I still have not been able to reproduce any of these errors. It seems the failure is due to zmq using a cython class without a proper __reduce__ method. I tried all the test codes above -- in the same (or similar) environments as specified... and I can not reproduce the error. @wendaomudong: is there something in your startup file (or how you start jupyter)?

@qingzma
Copy link

qingzma commented Oct 9, 2018

I have the same error, only in Jupyter notebook.
Works fine for Sublime Text....

@gerard0315
Copy link

gerard0315 commented Nov 23, 2018

same here, error only occurs when you do ipython = get_ipython() before dill.dump_session('notebook.db')

@gerard0315
Copy link

gerard0315 commented Nov 26, 2018

@mmckerns @dice89 a quick solution is to set ipython=None after you have finished your work with the ipython instance
i'm guessing the get_ipython() method is having conflict with dump_session() as both methods are trying to get the whole Python session

@idantene
Copy link

Running into the same issue (I got here from trying to pickle self.shell from Magic class).
If it helps at all, @mmckerns, I was playing with dill and ipython, eventually got the follwing traceback TypeError: can't pickle sqlite3.Connection objects:

$ ipython
import dill
dill.dumps(get_ipython)  # Minimal working example, but would fail on anything from `shell`, i.e. `get_ipython()`, `get_ipython().user_module`, etc
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-ca1fd54109a4> in <module>
----> 1 dill.dumps(get_ipython().user_module)

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in dumps(obj, protocol, byref, fmode, recurse)
    291     """pickle an object to a string"""
    292     file = StringIO()
--> 293     dump(obj, file, protocol, byref, fmode, recurse)#, strictio)
    294     return file.getvalue()
    295 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in dump(obj, file, protocol, byref, fmode, recurse)
    284         raise PicklingError(msg)
    285     else:
--> 286         pik.dump(obj)
    287     stack.clear()  # clear record of 'recursion-sensitive' pickled objects
    288     return

/usr/lib/python3.7/pickle.py in dump(self, obj)
    435         if self.proto >= 4:
    436             self.framer.start_framing()
--> 437         self.save(obj)
    438         self.write(STOP)
    439         self.framer.end_framing()

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in save_module(pickler, obj)
   1258                 + ["__builtins__", "__loader__"]]
   1259             pickler.save_reduce(_import_module, (obj.__name__,), obj=obj,
-> 1260                                 state=_main_dict)
   1261             log.info("# M1")
   1262         else:

/usr/lib/python3.7/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    660 
    661         if state is not None:
--> 662             save(state)
    663             write(BUILD)
    664 

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    891             # we only care about session the first pass thru
    892             pickler._session = False
--> 893         StockPickler.save_dict(pickler, obj)
    894         log.info("# D2")
    895     return

/usr/lib/python3.7/pickle.py in save_dict(self, obj)
    854 
    855         self.memoize(obj)
--> 856         self._batch_setitems(obj.items())
    857 
    858     dispatch[dict] = save_dict

/usr/lib/python3.7/pickle.py in _batch_setitems(self, items)
    880                 for k, v in tmp:
    881                     save(k)
--> 882                     save(v)
    883                 write(SETITEMS)
    884             elif n:

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    891             # we only care about session the first pass thru
    892             pickler._session = False
--> 893         StockPickler.save_dict(pickler, obj)
    894         log.info("# D2")
    895     return

/usr/lib/python3.7/pickle.py in save_dict(self, obj)
    854 
    855         self.memoize(obj)
--> 856         self._batch_setitems(obj.items())
    857 
    858     dispatch[dict] = save_dict

/usr/lib/python3.7/pickle.py in _batch_setitems(self, items)
    885                 k, v = tmp[0]
    886                 save(k)
--> 887                 save(v)
    888                 write(SETITEM)
    889             # else tmp is empty, and we're done

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in save_instancemethod0(pickler, obj)
   1067     log.info("Me: %s" % obj) #XXX: obj.__dict__ handled elsewhere?
   1068     if PY3:
-> 1069         pickler.save_reduce(MethodType, (obj.__func__, obj.__self__), obj=obj)
   1070     else:
   1071         pickler.save_reduce(MethodType, (obj.im_func, obj.im_self,

/usr/lib/python3.7/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    636         else:
    637             save(func)
--> 638             save(args)
    639             write(REDUCE)
    640 

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

/usr/lib/python3.7/pickle.py in save_tuple(self, obj)
    769         if n <= 3 and self.proto >= 2:
    770             for element in obj:
--> 771                 save(element)
    772             # Subtle.  Same as in the big comment below.
    773             if id(obj) in memo:

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    547 
    548         # Save the reduce() output and finally memoize the object
--> 549         self.save_reduce(obj=obj, *rv)
    550 
    551     def persistent_id(self, obj):

/usr/lib/python3.7/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    660 
    661         if state is not None:
--> 662             save(state)
    663             write(BUILD)
    664 

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    891             # we only care about session the first pass thru
    892             pickler._session = False
--> 893         StockPickler.save_dict(pickler, obj)
    894         log.info("# D2")
    895     return

/usr/lib/python3.7/pickle.py in save_dict(self, obj)
    854 
    855         self.memoize(obj)
--> 856         self._batch_setitems(obj.items())
    857 
    858     dispatch[dict] = save_dict

/usr/lib/python3.7/pickle.py in _batch_setitems(self, items)
    880                 for k, v in tmp:
    881                     save(k)
--> 882                     save(v)
    883                 write(SETITEMS)
    884             elif n:

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    891             # we only care about session the first pass thru
    892             pickler._session = False
--> 893         StockPickler.save_dict(pickler, obj)
    894         log.info("# D2")
    895     return

/usr/lib/python3.7/pickle.py in save_dict(self, obj)
    854 
    855         self.memoize(obj)
--> 856         self._batch_setitems(obj.items())
    857 
    858     dispatch[dict] = save_dict

/usr/lib/python3.7/pickle.py in _batch_setitems(self, items)
    880                 for k, v in tmp:
    881                     save(k)
--> 882                     save(v)
    883                 write(SETITEMS)
    884             elif n:

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    547 
    548         # Save the reduce() output and finally memoize the object
--> 549         self.save_reduce(obj=obj, *rv)
    550 
    551     def persistent_id(self, obj):

/usr/lib/python3.7/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
    660 
    661         if state is not None:
--> 662             save(state)
    663             write(BUILD)
    664 

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    891             # we only care about session the first pass thru
    892             pickler._session = False
--> 893         StockPickler.save_dict(pickler, obj)
    894         log.info("# D2")
    895     return

/usr/lib/python3.7/pickle.py in save_dict(self, obj)
    854 
    855         self.memoize(obj)
--> 856         self._batch_setitems(obj.items())
    857 
    858     dispatch[dict] = save_dict

/usr/lib/python3.7/pickle.py in _batch_setitems(self, items)
    880                 for k, v in tmp:
    881                     save(k)
--> 882                     save(v)
    883                 write(SETITEMS)
    884             elif n:

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    502         f = self.dispatch.get(t)
    503         if f is not None:
--> 504             f(self, obj) # Call unbound method with explicit self
    505             return
    506 

~/.hidden/.venv/lib/python3.7/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    891             # we only care about session the first pass thru
    892             pickler._session = False
--> 893         StockPickler.save_dict(pickler, obj)
    894         log.info("# D2")
    895     return

/usr/lib/python3.7/pickle.py in save_dict(self, obj)
    854 
    855         self.memoize(obj)
--> 856         self._batch_setitems(obj.items())
    857 
    858     dispatch[dict] = save_dict

/usr/lib/python3.7/pickle.py in _batch_setitems(self, items)
    880                 for k, v in tmp:
    881                     save(k)
--> 882                     save(v)
    883                 write(SETITEMS)
    884             elif n:

/usr/lib/python3.7/pickle.py in save(self, obj, save_persistent_id)
    522             reduce = getattr(obj, "__reduce_ex__", None)
    523             if reduce is not None:
--> 524                 rv = reduce(self.proto)
    525             else:
    526                 reduce = getattr(obj, "__reduce__", None)

TypeError: can't pickle sqlite3.Connection objects

@Boris-Molina
Copy link

Boris-Molina commented Sep 1, 2019

I get the same error. I have narrowed the problem to these lines:

Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import dill

In [2]: from sys import stderr

In [3]: dill.dump_session("NAME")

TypeError Traceback (most recent call last)
in
----> 1 dill.dump_session("NAME")

~\Anaconda3\envs\quant\lib\site-packages\dill_dill.py in dump_session(filename, main, byref)
391 pickler._recurse = False # disable pickling recursion for globals
392 pickler._session = True # is best indicator of when pickling a session
--> 393 pickler.dump(main)
394 finally:
395 if f is not filename: # If newly opened file

~\Anaconda3\envs\quant\lib\pickle.py in dump(self, obj)
435 if self.proto >= 4:
436 self.framer.start_framing()
--> 437 self.save(obj)
438 self.write(STOP)
439 self.framer.end_framing()

~\Anaconda3\envs\quant\lib\pickle.py in save(self, obj, save_persistent_id)
502 f = self.dispatch.get(t)
503 if f is not None:
--> 504 f(self, obj) # Call unbound method with explicit self
505 return
506

~\Anaconda3\envs\quant\lib\site-packages\dill_dill.py in save_module(pickler, obj)
1275 + ["builtins", "loader"]]
1276 pickler.save_reduce(_import_module, (obj.name,), obj=obj,
-> 1277 state=_main_dict)
1278 log.info("# M1")
1279 else:

~\Anaconda3\envs\quant\lib\pickle.py in save_reduce(self, func, args, state, listitems, dictitems, obj)
660
661 if state is not None:
--> 662 save(state)
663 write(BUILD)
664

~\Anaconda3\envs\quant\lib\pickle.py in save(self, obj, save_persistent_id)
502 f = self.dispatch.get(t)
503 if f is not None:
--> 504 f(self, obj) # Call unbound method with explicit self
505 return
506

~\Anaconda3\envs\quant\lib\site-packages\dill_dill.py in save_module_dict(pickler, obj)
908 # we only care about session the first pass thru
909 pickler._session = False
--> 910 StockPickler.save_dict(pickler, obj)
911 log.info("# D2")
912 return

~\Anaconda3\envs\quant\lib\pickle.py in save_dict(self, obj)
857
858 self.memoize(obj)
--> 859 self._batch_setitems(obj.items())
860
861 dispatch[dict] = save_dict

~\Anaconda3\envs\quant\lib\pickle.py in _batch_setitems(self, items)
883 for k, v in tmp:
884 save(k)
--> 885 save(v)
886 write(SETITEMS)
887 elif n:

~\Anaconda3\envs\quant\lib\pickle.py in save(self, obj, save_persistent_id)
522 reduce = getattr(obj, "reduce_ex", None)
523 if reduce is not None:
--> 524 rv = reduce(self.proto)
525 else:
526 reduce = getattr(obj, "reduce", None)

TypeError: cannot serialize '_io.TextIOWrapper' object

In [4]:

######################################################
However, same code RUN in Spyder console, produces the following error:

`
import dill

from sys import stderr

dill.dump_session("NAME")
Traceback (most recent call last):

File "", line 1, in
dill.dump_session("NAME")

File "C:\Users\molin\Anaconda3\envs\quant\lib\site-packages\dill_dill.py", line 393, in dump_session
pickler.dump(main)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 437, in dump
self.save(obj)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self

File "C:\Users\molin\Anaconda3\envs\quant\lib\site-packages\dill_dill.py", line 1277, in save_module
state=_main_dict)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 662, in save_reduce
save(state)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self

File "C:\Users\molin\Anaconda3\envs\quant\lib\site-packages\dill_dill.py", line 910, in save_module_dict
StockPickler.save_dict(pickler, obj)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 859, in save_dict
self._batch_setitems(obj.items())

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 885, in _batch_setitems
save(v)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 549, in save
self.save_reduce(obj=obj, *rv)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 662, in save_reduce
save(state)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self

File "C:\Users\molin\Anaconda3\envs\quant\lib\site-packages\dill_dill.py", line 910, in save_module_dict
StockPickler.save_dict(pickler, obj)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 859, in save_dict
self._batch_setitems(obj.items())

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 885, in _batch_setitems
save(v)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 549, in save
self.save_reduce(obj=obj, *rv)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 662, in save_reduce
save(state)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self

File "C:\Users\molin\Anaconda3\envs\quant\lib\site-packages\dill_dill.py", line 910, in save_module_dict
StockPickler.save_dict(pickler, obj)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 859, in save_dict
self._batch_setitems(obj.items())

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 885, in _batch_setitems
save(v)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self

File "C:\Users\molin\Anaconda3\envs\quant\lib\site-packages\dill_dill.py", line 910, in save_module_dict
StockPickler.save_dict(pickler, obj)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 859, in save_dict
self._batch_setitems(obj.items())

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 885, in _batch_setitems
save(v)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 549, in save
self.save_reduce(obj=obj, *rv)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 662, in save_reduce
save(state)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self

File "C:\Users\molin\Anaconda3\envs\quant\lib\site-packages\dill_dill.py", line 910, in save_module_dict
StockPickler.save_dict(pickler, obj)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 859, in save_dict
self._batch_setitems(obj.items())

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 885, in _batch_setitems
save(v)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 504, in save
f(self, obj) # Call unbound method with explicit self

File "C:\Users\molin\Anaconda3\envs\quant\lib\site-packages\dill_dill.py", line 910, in save_module_dict
StockPickler.save_dict(pickler, obj)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 859, in save_dict
self._batch_setitems(obj.items())

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 885, in _batch_setitems
save(v)

File "C:\Users\molin\Anaconda3\envs\quant\lib\pickle.py", line 524, in save
rv = reduce(self.proto)

File "stringsource", line 2, in zmq.backend.cython.context.Context.reduce_cython

TypeError: no default reduce due to non-trivial cinit

`

@Crispy13
Copy link

Any update?

@mhangaard
Copy link

mhangaard commented May 19, 2020

dill.dump gives me a similar error when attempting to dump an object containing an ipywidget.
(I can dump other objects with no problem)
I can also dump session just fine until i define a widget, i.e. until my session contains objects from ipywidget, at which point I get the same type of error message.

To reproduce
In a jupyter notebook

import ipywidgets
import dill
dill.dump_session('test.db')                                           # no problem.
mybutton = ipywidgets.Button(description="Test Dill")
dill.dump_session('test.db')                                           # error!
with open('test_widget.pkl', 'wb') as f:
    dill.dump(mybutton, f)                                             # error!

Environment
Python 3.8.2
[GCC 7.3.0] on linux
...
dill 0.3.1.1
nodejs 13.9.0
ipython 7.14.0
ipywidgets 7.5.1
JupyterLab v2.0.1
@jupyter-widgets/base v3.0.0 enabled OK
@jupyter-widgets/jupyterlab-manager v2.0.0 enabled OK
...

Log

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-f680605315ff> in <module>
      1 with open('test.pkl', 'wb') as f:
----> 2     dill.dump(mybutton, f)

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in dump(obj, file, protocol, byref, fmode, recurse, **kwds)
    257     _kwds = kwds.copy()
    258     _kwds.update(dict(byref=byref, fmode=fmode, recurse=recurse))
--> 259     Pickler(file, protocol, **_kwds).dump(obj)
    260     return
    261 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in dump(self, obj)
    443             raise PicklingError(msg)
    444         else:
--> 445             StockPickler.dump(self, obj)
    446         stack.clear()  # clear record of 'recursion-sensitive' pickled objects
    447         return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in dump(self, obj)
    483         if self.proto >= 4:
    484             self.framer.start_framing()
--> 485         self.save(obj)
    486         self.write(STOP)
    487         self.framer.end_framing()

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    599 
    600         # Save the reduce() output and finally memoize the object
--> 601         self.save_reduce(obj=obj, *rv)
    602 
    603     def persistent_id(self, obj):

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, state_setter, obj)
    713         if state is not None:
    714             if state_setter is None:
--> 715                 save(state)
    716                 write(BUILD)
    717             else:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    556             f = self.dispatch.get(t)
    557             if f is not None:
--> 558                 f(self, obj)  # Call unbound method with explicit self
    559                 return
    560 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    910             # we only care about session the first pass thru
    911             pickler._session = False
--> 912         StockPickler.save_dict(pickler, obj)
    913         log.info("# D2")
    914     return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_dict(self, obj)
    967 
    968         self.memoize(obj)
--> 969         self._batch_setitems(obj.items())
    970 
    971     dispatch[dict] = save_dict

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in _batch_setitems(self, items)
    993                 for k, v in tmp:
    994                     save(k)
--> 995                     save(v)
    996                 write(SETITEMS)
    997             elif n:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    556             f = self.dispatch.get(t)
    557             if f is not None:
--> 558                 f(self, obj)  # Call unbound method with explicit self
    559                 return
    560 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    910             # we only care about session the first pass thru
    911             pickler._session = False
--> 912         StockPickler.save_dict(pickler, obj)
    913         log.info("# D2")
    914     return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_dict(self, obj)
    967 
    968         self.memoize(obj)
--> 969         self._batch_setitems(obj.items())
    970 
    971     dispatch[dict] = save_dict

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in _batch_setitems(self, items)
    993                 for k, v in tmp:
    994                     save(k)
--> 995                     save(v)
    996                 write(SETITEMS)
    997             elif n:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    599 
    600         # Save the reduce() output and finally memoize the object
--> 601         self.save_reduce(obj=obj, *rv)
    602 
    603     def persistent_id(self, obj):

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, state_setter, obj)
    713         if state is not None:
    714             if state_setter is None:
--> 715                 save(state)
    716                 write(BUILD)
    717             else:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    556             f = self.dispatch.get(t)
    557             if f is not None:
--> 558                 f(self, obj)  # Call unbound method with explicit self
    559                 return
    560 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    910             # we only care about session the first pass thru
    911             pickler._session = False
--> 912         StockPickler.save_dict(pickler, obj)
    913         log.info("# D2")
    914     return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_dict(self, obj)
    967 
    968         self.memoize(obj)
--> 969         self._batch_setitems(obj.items())
    970 
    971     dispatch[dict] = save_dict

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in _batch_setitems(self, items)
    993                 for k, v in tmp:
    994                     save(k)
--> 995                     save(v)
    996                 write(SETITEMS)
    997             elif n:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    556             f = self.dispatch.get(t)
    557             if f is not None:
--> 558                 f(self, obj)  # Call unbound method with explicit self
    559                 return
    560 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    910             # we only care about session the first pass thru
    911             pickler._session = False
--> 912         StockPickler.save_dict(pickler, obj)
    913         log.info("# D2")
    914     return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_dict(self, obj)
    967 
    968         self.memoize(obj)
--> 969         self._batch_setitems(obj.items())
    970 
    971     dispatch[dict] = save_dict

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in _batch_setitems(self, items)
    993                 for k, v in tmp:
    994                     save(k)
--> 995                     save(v)
    996                 write(SETITEMS)
    997             elif n:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    599 
    600         # Save the reduce() output and finally memoize the object
--> 601         self.save_reduce(obj=obj, *rv)
    602 
    603     def persistent_id(self, obj):

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, state_setter, obj)
    713         if state is not None:
    714             if state_setter is None:
--> 715                 save(state)
    716                 write(BUILD)
    717             else:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    556             f = self.dispatch.get(t)
    557             if f is not None:
--> 558                 f(self, obj)  # Call unbound method with explicit self
    559                 return
    560 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    910             # we only care about session the first pass thru
    911             pickler._session = False
--> 912         StockPickler.save_dict(pickler, obj)
    913         log.info("# D2")
    914     return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_dict(self, obj)
    967 
    968         self.memoize(obj)
--> 969         self._batch_setitems(obj.items())
    970 
    971     dispatch[dict] = save_dict

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in _batch_setitems(self, items)
    993                 for k, v in tmp:
    994                     save(k)
--> 995                     save(v)
    996                 write(SETITEMS)
    997             elif n:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    556             f = self.dispatch.get(t)
    557             if f is not None:
--> 558                 f(self, obj)  # Call unbound method with explicit self
    559                 return
    560 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    910             # we only care about session the first pass thru
    911             pickler._session = False
--> 912         StockPickler.save_dict(pickler, obj)
    913         log.info("# D2")
    914     return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_dict(self, obj)
    967 
    968         self.memoize(obj)
--> 969         self._batch_setitems(obj.items())
    970 
    971     dispatch[dict] = save_dict

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in _batch_setitems(self, items)
    993                 for k, v in tmp:
    994                     save(k)
--> 995                     save(v)
    996                 write(SETITEMS)
    997             elif n:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    599 
    600         # Save the reduce() output and finally memoize the object
--> 601         self.save_reduce(obj=obj, *rv)
    602 
    603     def persistent_id(self, obj):

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, state_setter, obj)
    713         if state is not None:
    714             if state_setter is None:
--> 715                 save(state)
    716                 write(BUILD)
    717             else:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    556             f = self.dispatch.get(t)
    557             if f is not None:
--> 558                 f(self, obj)  # Call unbound method with explicit self
    559                 return
    560 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    910             # we only care about session the first pass thru
    911             pickler._session = False
--> 912         StockPickler.save_dict(pickler, obj)
    913         log.info("# D2")
    914     return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_dict(self, obj)
    967 
    968         self.memoize(obj)
--> 969         self._batch_setitems(obj.items())
    970 
    971     dispatch[dict] = save_dict

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in _batch_setitems(self, items)
    998                 k, v = tmp[0]
    999                 save(k)
-> 1000                 save(v)
   1001                 write(SETITEM)
   1002             # else tmp is empty, and we're done

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    599 
    600         # Save the reduce() output and finally memoize the object
--> 601         self.save_reduce(obj=obj, *rv)
    602 
    603     def persistent_id(self, obj):

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_reduce(self, func, args, state, listitems, dictitems, state_setter, obj)
    713         if state is not None:
    714             if state_setter is None:
--> 715                 save(state)
    716                 write(BUILD)
    717             else:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    556             f = self.dispatch.get(t)
    557             if f is not None:
--> 558                 f(self, obj)  # Call unbound method with explicit self
    559                 return
    560 

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/dill/_dill.py in save_module_dict(pickler, obj)
    910             # we only care about session the first pass thru
    911             pickler._session = False
--> 912         StockPickler.save_dict(pickler, obj)
    913         log.info("# D2")
    914     return

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save_dict(self, obj)
    967 
    968         self.memoize(obj)
--> 969         self._batch_setitems(obj.items())
    970 
    971     dispatch[dict] = save_dict

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in _batch_setitems(self, items)
    993                 for k, v in tmp:
    994                     save(k)
--> 995                     save(v)
    996                 write(SETITEMS)
    997             elif n:

~/anaconda3/envs/openbabelenv/lib/python3.8/pickle.py in save(self, obj, save_persistent_id)
    574                 reduce = getattr(obj, "__reduce_ex__", None)
    575                 if reduce is not None:
--> 576                     rv = reduce(self.proto)
    577                 else:
    578                     reduce = getattr(obj, "__reduce__", None)

~/anaconda3/envs/openbabelenv/lib/python3.8/site-packages/zmq/backend/cython/socket.cpython-38-x86_64-linux-gnu.so in zmq.backend.cython.socket.Socket.__reduce_cython__()

TypeError: no default __reduce__ due to non-trivial __cinit__

@davidgilbertson
Copy link

There is a related issue in PyCharm. This error seems to occur when running a script in the Python Console with IPython, but not when pasting the same code in the Python Console.

I don't really know what's going on, but it seems to be in the area of Dill where it's checking for copied globals and adding them to postproc_list, and only happens when using a forked pickler.

@mmckerns this is my comment to the PyCharm folks, perhaps it gives you a clue as to why the error occurs?

I've been digging for a few hours and it seems to be that dill (a pickler) is trying to pickle the globals. The globals contain the IPython history manager, which has a SQLite DB connection, and that errors.

But I can't work out why sometimes it tries to pickle globals and sometimes not. Or maybe it always does and sometimes it works and sometimes it doesn't.

Maybe you will recognise something in this code: dill/_dill.py at master · uqfoundation/dill (github.com)

This is where Dill is adding globs_copy to the list of things that should be pickled, and at this point, that object contains (deeply nested) the IPython history manager and it's DB connection.

I've tried with the latest dill version: 0.3.6 and it still occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.