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 

相关内容

热门资讯

魅族降系统安卓13系统,探索无... 你知道吗?最近手机圈里可是炸开了锅,魅族这个品牌竟然悄悄地给自家手机升级了安卓13系统!这可真是让人...
安卓手机查看系统信息,系统信息... 你有没有想过,你的安卓手机里藏着多少秘密?别小看那小小的屏幕,它可是个信息宝库呢!今天,就让我带你一...
安卓7.0系统相机崩溃,原因分... 最近是不是你也遇到了安卓7.0系统相机崩溃的烦恼?别急,让我来给你详细说说这个让人头疼的问题,让你一...
安卓u子系统系统u盘,Andr... 你有没有想过,你的安卓手机里有一个神秘的“U子系统”,它就像一个隐藏的宝藏,等待着你去探索。今天,就...
联想怎么下载安卓系统,安卓系统... 你有没有想过,你的联想手机或者平板,有一天也能装上安卓系统的全新面貌呢?没错,今天就要来手把手教你如...
查老式安卓系统版本,展望未来 你有没有发现,手机里的安卓系统版本有时候就像是个神秘的宝藏,藏着许多不为人知的秘密呢?今天,就让我带...
核酸录入安卓机系统,基于安卓系... 你有没有想过,那些看似简单的核酸检测,背后竟然有这么复杂的“大脑”——安卓机系统?没错,就是那个我们...
高邮苹果刷安卓系统,安卓系统助... 你有没有想过,那些我们平时吃的苹果,竟然也能刷上安卓系统?听起来是不是有点不可思议?没错,今天就要带...
安卓系统的ar眼镜,安卓系统赋... 你有没有想过,未来科技的世界里,眼镜不仅仅是用来看清世界的工具,还能成为你的得力助手?没错,说的就是...
怎样删安卓系统垃圾,揭秘安卓系... 手机里的安卓系统是不是越来越卡了?是不是觉得那些垃圾文件占据了太多空间,让你头疼不已?别急,今天就来...
畅享10安卓系统,畅享智能生活... 你有没有听说啊?最近安卓系统又来了一次大升级,这次可是直接跳到了10版本,听起来是不是很酷炫?没错,...
安卓怎么刷荣耀系统,轻松刷入荣... 你有没有想过,你的安卓手机能不能来点不一样的风采呢?没错,就是刷机!今天,就让我来带你一起探索如何给...
安卓系统游戏占用内存,揭秘内存... 手机里的游戏是不是越来越多了?每次打开,那内存占用简直让人头疼!今天,就让我来和你聊聊安卓系统游戏占...
安卓驱动系统开发,基于安卓驱动... 你有没有想过,你的安卓手机里那些神奇的驱动系统是怎么来的?没错,就是那些让手机能够顺畅运行各种应用的...
安卓更新系统14,系统革新与未... 你知道吗?最近安卓系统又来了一次大变身,那就是安卓更新系统14!这可不是一个小打小闹的更新,而是带来...
安卓手机按键刷系统,轻松实现系... 你有没有想过,你的安卓手机按键居然也能刷系统呢?没错,你没听错!今天,就让我带你一起探索这个神奇的小...
安卓系统电脑下载图片,轻松获取... 你有没有想过,在电脑上下载图片竟然也能变得如此简单?没错,就是那个我们每天都离不开的安卓系统,它现在...
安卓系统电池描述文件,深度解析... 你有没有发现,手机用着用着,电池就有点不给力了?别急,今天就来给你揭秘安卓系统里的那个神秘角色——电...
鸿蒙系统基于安卓内核,融合与创... 你知道吗?最近科技圈里有个大新闻,那就是华为的鸿蒙系统。没错,就是那个让安卓和iOS都黯然失色的操作...
云os系统装安卓,轻松实现安卓... 你有没有想过,你的手机或者平板电脑,其实可以像电脑一样,装上各种各样的操作系统呢?没错,今天就要来聊...