pickle/zodb: how to handle moving .py files with class definitions?

I’m using ZODB which, as I understand it, uses pickle to store class instances. I’m doing a bit of refactoring where I want to split my models.py file into several files. However, if I do this, I don’t think pickle will be able to find the class definitions, and thus won’t be able to load the objects that I already have stored in the database. What’s the best way to handle this problem?

Best answer

You can create aliases; because one models.py modules is being split into multiple new modules you can only do this by importing your classes into the old location.

Both methods cause new copies of your instance pickles to refer to the new location; if you can force a write on all instances of the moved classes you don’t need to retain the aliases. You can do this by setting _p_changed to True on your instances that you want to be written again.

So, to create the aliases, import your moved classes in the old location:

from newmodule1 import MyClass1, MyClass2
from newmodule2 import MyClass3

If you only rename a module (so the same classes all are found in one new location, could be a set of imports themselves), you can also create a sys.modules entry for the old name:

import sys
import newmodule

sys.modules['full.path.to.old.module] = newmodule