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 

相关内容

热门资讯

氨安装系统保压,氨安装系统保压... 氨安装系统保压的重要性与实施方法氨制冷系统在现代工业和商业制冷领域应用广泛,其高效、环保的特点使其成...
北斗系统安装后续,北斗系统安装... 北斗系统安装进展与未来展望随着我国北斗卫星导航系统的不断完善和普及,北斗系统的安装和应用已经深入到各...
把手机系统安装在卡里,如何将手... 如何将手机系统安装在SD卡里?随着智能手机的普及,用户对手机存储空间的需求越来越大。为了提高手机的存...
保山智能音响系统安装,保山智能... 保山智能音响系统安装——打造家庭影院新体验一、选择合适的智能音响系统在安装智能音响系统之前,首先需要...
安装系统专业版10 Windows 10 专业版系统安装教程一、准备工作在开始安装 Windows 10 专业版系统之前...
安卓系统怎么安装zip,安卓系... 安卓系统安装zip文件全攻略随着智能手机的普及,越来越多的用户开始接触和使用安卓系统。在下载应用或游...
报警系统安装验收规范 报警系统安装验收规范随着社会的发展和科技的进步,报警系统在公共安全、家庭安全等领域扮演着越来越重要的...
安卓系统安装会唱歌,安卓系统安... 安卓系统安装会唱歌应用,让你的手机也能唱歌跳舞随着智能手机的普及,安卓系统因其开放性和丰富的应用生态...
安装下载gps定位系统,gps... GPS定位系统安装与下载指南一、GPS定位系统简介GPS定位系统,全称为全球定位系统(Global ...
清风u盘安装系统,轻松实现电脑... 清风U盘安装系统教程:轻松实现电脑系统重装一、准备工具与材料在开始安装系统之前,我们需要准备以下工具...
安装在移动盘的系统,如何在移动... 如何在移动硬盘上安装操作系统随着移动硬盘存储能力的提升,越来越多的用户选择将操作系统安装在移动硬盘上...
安卓系统安装cad,安卓系统上... 安卓系统上安装CAD软件的详细教程随着移动办公的普及,越来越多的专业人士开始使用安卓手机进行工作。C...
北京监控系统安装维修,保障安全... 北京监控系统安装维修:保障安全,提升效率随着社会经济的发展,人们对安全防范的需求日益增长。监控系统作...
安装影子系统电脑蓝屏,安装影子... 安装影子系统后电脑蓝屏的解决方法及原因分析影子系统作为一种强大的系统保护软件,能够在一定程度上隔离操...
安装系统找不到本地介质,安装系... 安装系统时找不到本地介质的原因及解决方法在电脑使用过程中,我们可能会遇到重装系统的情况。然而,在安装...
庆阳全景系统门窗安装,庆阳全景... 庆阳全景系统门窗安装,打造现代家居新风尚一、庆阳全景系统门窗的特点庆阳全景系统门窗采用高强度的铝合金...
帮助安装电脑系统,轻松掌握系统... 电脑系统安装教程:轻松掌握系统安装方法一、准备工作在开始安装电脑系统之前,我们需要做好以下准备工作:...
奥迪安卓系统安装,轻松升级车载... 奥迪安卓系统安装指南:轻松升级车载娱乐体验一、了解奥迪安卓系统奥迪安卓系统是基于Android开源平...
安卓安装ppc系统,安卓系统安... 安卓系统安装到PPC设备上的详细教程一、准备工作在开始安装安卓系统之前,您需要做好以下准备工作: ...
报表系统安装包,轻松实现数据可... 全面解析报表系统安装包:轻松实现数据可视化与报表定制随着大数据时代的到来,数据已经成为企业决策的重要...