The python docs say:
pickle can save and restore class instances transparently, however the class definition must be importable and live in the same module as when the object was stored.
Could I put a pickler/unpickler in the module where the class was stored?
Or do I have to put the class in the module? And how?
I’m trying to pickle/unpickle a object from a class in a external module.
The python docs say: pickle can save and restore class instances transparently, however the
Share
You can pickle any python class instance, as long as the
picklemodule can import it again when you load the pickle.It doesn’t matter where in your python code you use
load()ordump(), it only matters if the data that you are going to pickle can be retrieved again later on, by importing them from the same location.So, if you have a module
foo.barwith a classSpamin it, then as long as you can dofrom foo.bar import Spamyou can pickle instances of that class, becausepicklecan later on load that class again from the same module.