Establishing why an object can't be pickled

前端 未结 4 2025
小蘑菇
小蘑菇 2021-01-11 14:37

I\'m receiving an object, t, from an api of type Object. I am unable to pickle it, getting the error:

  File \"p.py\", line 55, in          


        
4条回答
  •  攒了一身酷
    2021-01-11 14:56

    I would use dill, which has tools to investigate what inside an object causes your target object to not be picklable. See this answer for an example: Good example of BadItem in Dill Module, and this Q&A for an example of the detection tools in real use: pandas.algos._return_false causes PicklingError with dill.dump_session on CentOS.

    >>> import dill
    >>> x = iter([1,2,3,4])
    >>> d = {'x':x}
    >>> # we check for unpicklable items in d (i.e. the iterator x)
    >>> dill.detect.baditems(d)
    []
    >>> # note that nothing inside of the iterator is unpicklable!
    >>> dill.detect.baditems(x)
    []
    

    However, the most common starting point is to use trace:

    >>> dill.detect.trace(True)
    >>> dill.detect.errors(d)
    D2: 
    T4: 
    PicklingError("Can't pickle : it's not found as __builtin__.listiterator",)
    >>> 
    

    dill also has functionality to trace pointers referrers and referents to objects, so you can build a hierarchy of how objects refer to each other. See: https://github.com/uqfoundation/dill/issues/58

    Alternately, there's also: cloudpickle.py and debugpickle.py, which are for the most part no longer developed. I'm the dill author, and hope to soon merge any functionality in these codes that is missing in dill.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题