简体   繁体   English

酸洗时“无法获取属性” Object

[英]"Can't get attribute" when Pickling Object

My save code is written as so in a file called 'MachLearn.py':我的保存代码是这样写在一个名为“MachLearn.py”的文件中的:

Whilst looking for this code I find out I accidentally overwrote it with an old version:/.在寻找这段代码时,我发现我不小心用旧版本覆盖了它:/. It was essentially structured like this:它的结构基本上是这样的:

class attibuteGenerator():
   def __init__(self):
   #more class stuff

   def returnAttributes(self, rating, position):
       #func stuff

if __name__ = "__main__":
    ag = attributeGenerator():
    with open('attributeGenerator_pickle', 'wb') as f:
        pickle.dump(f, ag)

My open code is written as so in a file called "mainGame.py"我的开放代码是这样写在一个名为“mainGame.py”的文件中的

def main():
    with open('attributeGenerator_pickle', 'rb') as f:
        bob = pickle.load(f)
        print(bob.returnAttributes(34, "LW"))

if __name__ == "__main__":
    main()

Is there an issue with my code?我的代码有问题吗? It's giving:它给: 在此处输入图像描述

This type of error arises when you attempt to unpickle an object, such as a class instance, and you don't have access to the relevant class in your current Python session. This type of error arises when you attempt to unpickle an object, such as a class instance, and you don't have access to the relevant class in your current Python session. You should reimport the packages you were using to generate the objects that you pickled.您应该重新导入您用来生成您腌制的对象的包。

The following minimal script will reproduce this problem:以下最小脚本将重现此问题:

import pickle

class my_class():
    def __init__(self):
        self.x = 2

inst = my_class()
with open('file.dat', 'wb') as f:
    pickle.dump(inst, f); f.close()

del my_class # Deletes the class and causes the problem

with open('file.dat', 'rb') as f:
    new_inst = pickle.load(f); f.close()

with error:有错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-6571eae10a31> in <module>
     12 
     13 with open('file.dat', 'rb') as f:
---> 14     new_inst = pickle.load(f); f.close()

AttributeError: Can't get attribute 'my_class' on <module '__main__'>

This can be resolved by removing the line: del my_class .这可以通过删除以下行来解决: del my_class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 酸洗Matplotlib情节提升PicklingError:不能腌制&#39;RendererAgg&#39;对象 - Pickling Matplotlib plot raising PicklingError: Can't pickle 'RendererAgg' object 酸洗错误:不能泡菜<type 'function'> - Pickling error: Can't pickle <type 'function'> 多处理:池和泡菜错误—泡菜错误:不能泡菜 <type 'instancemethod'> :属性查找__builtin __。instancemethod失败 - Multiprocessing: Pool and pickle Error — Pickling Error: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed 无法访问python lmdb,&#39;对象没有属性&#39;Environment&#39; - can't get access to python lmdb , ' object has no attribute 'Environment'' 酸洗,压缩Python对象并存储在SQL中时信息丢失 - Information lost when pickling, compressing Python object and storing in SQL 当变量名与类型名不匹配时,namedtuple酸洗失败 - namedtuple pickling fails when variable name doesn't match typename rembg library can't open 'i' 错误和 pickling 错误 - rembg library can't open 'i' error and also pickling error 在 appengine 上酸洗 object - Pickling an object on appengine 在pickle一个类时,我在cython中的python中得到了不同的行为 - When pickling a class I get different behavior in python that in cython 腌制“被拘留”的物体 - Pickling a “interned” object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM