Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: can't pickle module objects error

Tags:

I'm trying to pickle a big class and getting

TypeError: can't pickle module objects

despite looking around the web, I can't exactly figure out what this means. and I'm not sure which module object is causing the trouble. is there a way to find the culprit? the stack trace doesn't seem to indicate anything.

like image 225
adum Avatar asked May 07 '10 18:05

adum


People also ask

Can you pickle objects Python?

Python comes with a built-in package, known as pickle , that can be used to perform pickling and unpickling operations. Pickling and unpickling in Python is the process that is used to describe the conversion of objects into byte streams and vice versa - serialization and deserialization, using Python's pickle module.

What Cannot be pickled in Python?

With pickle protocol v1, you cannot pickle open file objects, network connections, or database connections.

What is a pickling error Python?

PicklingError is used when the process of pickling one or more python objects is not able to proceed. pickle.UnpicklingError. This is exception pickle. UnpicklingError is raised when the process of converting a pickled byte stream into python object hierarchie(s) fails.

What is module object in Python?

A module is a Python object with arbitrarily named attributes that you can bind and reference. The Python code for a module named aname normally resides in a file named aname.py, as covered in Module Loading. In Python, modules are objects (values) and are handled like other objects.


1 Answers

Python's inability to pickle module objects is the real problem. Is there a good reason? I don't think so. Having module objects unpicklable contributes to the frailty of python as a parallel / asynchronous language. If you want to pickle module objects, or almost anything in python, then use dill.

Python 3.2.5 (default, May 19 2013, 14:25:55)  [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import dill >>> import os >>> dill.dumps(os) b'\x80\x03cdill.dill\n_import_module\nq\x00X\x02\x00\x00\x00osq\x01\x85q\x02Rq\x03.' >>> >>> >>> # and for parlor tricks... >>> class Foo(object): ...   x = 100 ...   def __call__(self, f): ...     def bar(y): ...       return f(self.x) + y ...     return bar ...  >>> @Foo() ... def do_thing(x): ...   return x ...  >>> do_thing(3) 103  >>> dill.loads(dill.dumps(do_thing))(3) 103 >>>  

Get dill here: https://github.com/uqfoundation/dill

like image 171
Mike McKerns Avatar answered Oct 04 '22 04:10

Mike McKerns