Python pickling after changing a module’s directory

As pickle’s docs say, in order to save and restore a class instance (actually a function, too), you must respect certain constraints:

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

whyteboard.tools is not the “the same module as” tools (even though it can be imported by import tools by other modules in the same package, it ends up in sys.modules as sys.modules['whyteboard.tools']: this is absolutely crucial, otherwise the same module imported by one in the same package vs one in another package would end up with multiple and possibly conflicting entries!).

If your pickle files are in a good/advanced format (as opposed to the old ascii format that’s the default only for compatibility reasons), migrating them once you perform such changes may in fact not be quite as trivial as “editing the file” (which is binary &c…!), despite what another answer suggests. I suggest that, instead, you make a little “pickle-migrating script”: let it patch sys.modules like this…:

import sys
from whyteboard import tools

sys.modules['tools'] = tools

and then cPickle.load each file, del sys.modules['tools'], and cPickle.dump each loaded object back to file: that temporary extra entry in sys.modules should let the pickles load successfully, then dumping them again should be using the right module-name for the instances’ classes (removing that extra entry should make sure of that).

Leave a Comment