Load pickled object in different file - Attribute error

前端 未结 2 1067
名媛妹妹
名媛妹妹 2021-02-02 10:30

I have some trouble with loading a pickled file in a module that is different from the module where I pickled the file. I am aware of the following thread: Unable to load files

2条回答
  •  走了就别回头了
    2021-02-02 11:06

    I had a similar problem and only just realized the differences between our implementations.

    Your file structure:

    • util.py
      • define pickle functions
    • class_def.py
      • import util
      • define class
      • make instance
      • call save pickle
    • process.py
      • import util
      • load pickle

    My mistake (using your file names) was first:

    • util_and_class.py
      • define class
      • define pickle funcs
      • make instance
      • call save pickle
    • process.py
      • import util_and_class
      • call load pickle << ERROR

    What solved my pickle import problem:

    • util_and_class.py
      • define class
      • define pickle funcs
    • pickle_init.py
      • import util_and_class
      • make instance
      • call save pickle
    • process.py
      • call load pickle

    This had the welcomed side effect that I didn't need to import the util_and_class file as it's baked into the pickle file. Calling the instance and saving the pickle in a separate file resolved the __name__ issue of "loading a pickled file in a module that is different from the module where I pickled the file."

提交回复
热议问题