AttributeError when unpickling an object

前端 未结 2 2025
感情败类
感情败类 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.

提交回复
热议问题