2

用于dill序列化类时:

import dill, pickle

class project(object):
    def __init__(self, name='', folder='', user_id='', version=-1 ):
        self.name, self.folder, self.user_id, self.version = name, folder, user_id, version
# Other code, not shown, since Big class .......

dill.dump(proj1, open('test_dill.pkl', 'w'), byref=False )

我收到这条消息:

PicklingError: Can't pickle <built-in function raw_input>: it's not the same object as __builtin__.raw_input


  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 236, in dump
    pik.dump(obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 224, in dump
    self.save(obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 401, in save_reduce
    save(args)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 554, in save_tuple
    save(element)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 401, in save_reduce
    save(args)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 554, in save_tuple
    save(element)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 401, in save_reduce
    save(args)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 554, in save_tuple
    save(element)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 401, in save_reduce
    save(args)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 554, in save_tuple
    save(element)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 606, in save_list
    self._batch_appends(iter(obj))

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 642, in _batch_appends
    save(tmp[0])

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 425, in save_reduce
    save(state)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 655, in save_dict
    self._batch_setitems(obj.iteritems())

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 687, in _batch_setitems
    save(v)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "D:\_devs\Python01\Anaconda27\lib\site-packages\dill\dill.py", line 993, in save_builtin_method
    StockPickler.save_global(pickler, obj)

  File "D:\_devs\Python01\Anaconda27\lib\pickle.py", line 759, in save_global
    (obj, module, name))

PicklingError: Can't pickle <built-in function raw_input>: it's not the same object as __builtin__.raw_input

我不确定是否可以通过代码修改内置函数......
这听起来很神秘。

4

1 回答 1

3

我是dill作者。不同的设置(例如byref)用于修改包含在dump. pickle,因此dill,使用递归对象序列化,这意味着当一个对象引用另一个对象时,它可能(或可能不)序列化它,基于序列化设置。 pickle通常通过引用( byref=True) 序列化函数和类,这意味着它们仅在导入时和按名称序列化。dill有几个设置,例如byref=False(序列化函数和类对象而不是通过引用)。还有recurse,它处理全局引用是如何序列化的——recurse=False将所有全局变量序列化为一个字典,而recurse=True仅序列化全局变量中的引用项和这些项引用的项,依此类推。

简而言之,无论您要序列化什么,都有一些全局引用,这些引用会触发递归序列化,直到遇到dill无法腌制的东西。

以下是您可以尝试的一些事情:

  1. 将文件中的对象分离到单独的文件中——这使您可以更多地依赖于引用酸洗。
  2. 使用不同的设置,例如recurse=True,更改哪些对象dill尝试与您的目标对象进行序列化。您可以使用 全局设置设置dill.settings
  3. 将序列化辅助方法添加到您的类,告知pickle如何序列化您的类。
  4. 重构你的类(不知何故,因为我看不到它的内容)以避免序列化问题。

由于您尚未发布演示错误的工作示例,因此很难更深入地回答。

于 2016-11-23T19:12:19.577 回答