TypeError: cannot pickle ‘module‘ object
创始人
2024-05-30 16:16:07
0

创建python对象时报错:
TypeError: cannot pickle 'module' object

原因:

很大可能是类成员错误的使用了第三方包(别名)等,具体排查方法可参考:

import redisimport pickle
from pprint import pformat as pfdef pickle_trick(obj, max_depth=10):output = {}if max_depth <= 0:return outputtry:pickle.dumps(obj)except (pickle.PicklingError, TypeError) as e:failing_children = []if hasattr(obj, "__dict__"):for k, v in obj.__dict__.items():result = pickle_trick(v, max_depth=max_depth - 1)if result:failing_children.append(result)output = {"fail": obj, "err": e, "depth": max_depth, "failing_children": failing_children}return outputif __name__ == "__main__":r = redis.Redis()print(pf(pickle_trick(r)))

另外附上说明: 

What can be pickled and unpickled?

The following types can be pickled:

  • None, True, and False

  • integers, floating point numbers, complex numbers

  • strings, bytes, bytearrays

  • tuples, lists, sets, and dictionaries containing only picklable objects

  • functions defined at the top level of a module (using def, not lambda)

  • built-in functions defined at the top level of a module

  • classes that are defined at the top level of a module

  • instances of such classes whose __dict__ or the result of calling __getstate__() is picklable (see section Pickling Class Instances for details).

As you can see, modules are not part of this list. Note, that this is also true when using deepcopy and not only for the pickle module, as stated in the documentation of deepcopy:

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.

A possible workaround is using the @property decorator instead of an attribute. For example, this should work:

    import numpy as npimport pickleclass Foo():@propertydef module(self):return npfoo = Foo()with open('test.out', 'wb') as f:pickle.dump(foo, f)

参考资料:

Python: can't pickle module objects error - Stack Overflow 

相关内容

热门资讯

安卓系统限制投屏,揭秘安卓系统... 你是不是也遇到过这种情况?手机屏幕上精彩纷呈,可就是没有声音?别急,今天就来给你揭秘安卓系统限制投屏...
塞班系统能改安卓系统吗,迈向安... 你有没有想过,如果有一天你的老式塞班手机突然焕发了第二春,变成了一个安卓小能手呢?想象那会是多么神奇...
安卓系统大_删除哪里,删除哪些... 手机里的安卓系统是不是越来越臃肿了?别急,今天就来教你怎么给安卓系统做个大瘦身,让它跑得更快,用得更...
安卓软件系统修改工具,安卓软件... 你有没有想过,你的安卓手机里那些熟悉的软件,其实可以变得更有个性,更有趣呢?没错,就是通过一些神奇的...
安卓系统自动登录blued,轻... 你有没有发现,每次打开手机,安卓系统的自动登录功能真是方便得让人忍不住想给它点个赞!尤其是像blue...
安卓系统6.0升8.0,系统升... 亲爱的手机控们,你们是不是也和我一样,对手机系统升级这件事儿充满了好奇和期待呢?今天,我就要来和你聊...
安卓系统添加库文件,轻松实现功... 你有没有想过,你的安卓手机里那些酷炫的应用,其实都是靠那些神秘的库文件在背后默默支撑呢?今天,就让我...
安卓系统级别如何查看,具体实现... 亲爱的手机控们,你是否曾好奇过自己手机的安卓系统版本?想知道它是不是最新的,或者只是想了解一下它的“...
递皂者安卓系统,功能解析与使用... 你知道吗?在手机应用的世界里,有一个小家伙最近可是火得一塌糊涂,它就是——递皂者安卓系统。是不是听起...
谷歌正在开发安卓系统,安卓系统... 哇,你知道吗?最近科技圈可是炸开了锅,因为谷歌这个科技巨头竟然在秘密研发一款全新的笔记本电脑,而且这...
如何升级安卓系统手机,安卓系统... 升级安卓系统,让你的手机焕然一新!亲爱的手机控们,你是否也期待着手机系统升级,让爱机焕发出新的活力呢...
手机安卓进不了系统,原因排查与... 手机突然开不了机,这可怎么办呢?别急,让我来给你支个招儿!手机安卓进不了系统,这可能是很多小伙伴都遇...
安卓系统有后门不,安卓系统后门... 安卓系统有后门不?在当今数字化的世界里,智能手机已经成为我们生活中不可或缺的一部分。无论是工作、学习...
车载ce系统改安卓,探索安卓系... 你有没有想过,你的车载CE系统其实也可以变身成安卓系统呢?没错,就是那个我们日常使用的安卓系统,现在...
苹果系统比安卓耗电,高效与耗电... 你有没有发现,同样是玩手机,苹果手机和安卓手机在电量消耗上好像有点不一样呢?有时候,你拿着苹果手机,...
红米安卓系统桌面,个性化与便捷... 哇塞,你有没有发现,你的红米手机桌面最近变得超级酷炫?没错,就是那个每天陪你起床、陪你睡觉的红米安卓...
安卓系统里清理垃圾,提升手机流... 亲爱的手机,你最近是不是有点儿“胖”了呢?别担心,今天就来给你做个“瘦身操”,让你的安卓系统焕然一新...
安卓系统有趣的游戏,盘点那些令... 你知道吗?安卓系统上的游戏世界可是个五彩斑斓的乐园,里面藏着无数有趣的游戏,让人玩得不亦乐乎。今天,...
安卓 改 xp系统教程,模拟器... 你有没有想过,在手机上也能玩玩那个经典的Windows XP系统呢?没错,就是那个曾经陪伴我们度过无...
安卓系统能锁住吗,全方位解锁攻... 手机屏幕锁住了,是不是感觉心里有点慌?别急,今天就来给你揭秘安卓系统的锁屏秘密,让你轻松解锁,重获手...