AttributeError when unpickling an object

前端 未结 2 2014
感情败类
感情败类 2020-12-14 08:56

I\'m trying to pickle an instance of a class in one module, and unpickle it in another.

Here\'s where I pickle:

import cPickle

def pickleObject():
          


        
相关标签:
2条回答
  • 2020-12-14 09:17

    Jeremy Brown had the right answer, here is a more concrete version of the same point:

    import cPickle
    import myFooDefiningModule
    def pickleObject():
        object = myFooDefiningModule.Foo()
        savefile = open('path/to/file', 'w')
        cPickle.dump(object, savefile)
    

    and:

    import cPickle
    import myFooDefiningModule
    savefile = open('path/to/file', 'r')
    object = cPickle.load(savefile)
    

    such that Foo lives in the same namespace in each piece of code.

    0 讨论(0)
  • 2020-12-14 09:23

    class Foo must be importable via the same path in the unpickling environment so that the pickled object can be reinstantiated.

    I think your issue is that you define Foo in the module that you are executing as main (__name__ == "__main__"). Pickle will serialize the path (not the class object/definition!!!) to Foo as being in the main module. Foo is not an attribute of the main unpickle script.

    In this example, you could redefine class Foo in the unpickling script and it should unpickle just fine. But the intention is really to have a common library that is shared between the two scripts that will be available by the same path. Example: define Foo in foo.py

    Simple Example:

    $PROJECT_DIR/foo.py

    class Foo(object):
        pass
    

    $PROJECT_DIR/picklefoo.py

    import cPickle
    from foo import Foo
    
    def pickleObject():
        obj = Foo()
        savefile = open('pickle.txt', 'w')
        cPickle.dump(obj, savefile, cPickle.HIGHEST_PROTOCOL)
    
    
    pickleObject()
    

    $PROJECT_DIR/unpicklefoo.py

    import cPickle
    
    savefile = open('pickle.txt', 'r')
    obj = cPickle.load(savefile)
    ...
    
    0 讨论(0)
提交回复
热议问题