0

As long as I import class from my own library, my pickle failed, why? For example:

def fun():
   print(1)
if __name__=="__main__":
   import pickle
   pickle.dump(fun, open('save', 'wb'))

The script above works fine, but if I add from xxx.yyy import zzz (my class from my local library):

_pickle.PicklingError: Can't pickle <function fun at 0x7f5e40665e18>: attribute lookup fun on __main__ failed occurs.

Why? Is there any workaround? By the way, the problem originally occurs when I was trying to use torch.multiprocessing for distributed training, then I reduce the problem to above.

YoarkYANG
  • 303
  • 2
  • 18
  • 1
    Does this answer your question? [Python multiprocessing PicklingError: Can't pickle ](https://stackoverflow.com/questions/8804830/python-multiprocessing-picklingerror-cant-pickle-type-function) – GaiusJulius Dec 22 '20 at 10:59
  • @GaiusJulius no, i saw that one before, and i checked pickle doc that describes what cant be pickled. i am confused why i could import class from other libs and pickle stuff, but once i import a class from my library, i cant pickle, and the thing i was trying to pickle has nothing to do with the class i imported – YoarkYANG Dec 22 '20 at 17:53

1 Answers1

0

The script above works fine, but if I add from xxx.yyy import zzz (my class from my local library):

_pickle.PicklingError: Can't pickle <function fun at 0x7f5e40665e18>: attribute lookup fun on main failed occurs.

I just meet the same error, after I comment the import ipdb, the error is gone.

tlentali
  • 3,407
  • 2
  • 14
  • 21