I've recently changed my program's directory layout: before, I had all my modules inside the "main" folder. Now, I've moved them into a directory named after the program, and placed an __init__.py
there to make a package.
Now I have a single .py file in my main directory that is used to launch my program, which is much neater.
Anyway, trying to load in pickled files from previous versions of my program is failing. I'm getting, "ImportError: No module named tools" - which I guess is because my module was previously in the main folder, and now it's in whyteboard.tools, not simply plain tools. However, the code that is importing in the tools module lives in the same directory as it, so I doubt there's a need to specify a package.
So, my program directory looks something like this:
whyteboard-0.39.4
-->whyteboard.py
-->README.txt
-->CHANGELOG.txt
---->whyteboard/
---->whyteboard/__init__.py
---->whyteboard/gui.py
---->whyteboard/tools.py
whyteboard.py launches a block of code from whyteboard/gui.py, that fires up the GUI. This pickling problem definitely wasn't happening before the directory re-organizing.
Happened to me, solved it by adding the new location of the module to sys.path before loading pickle:
This is the normal behavior of pickle, unpickled objects need to have their defining module importable.
You should be able to change the modules path (i.e. from
tools
towhyteboard.tools
) by editing the pickled files, as they are normally simple text files.This can be done with a custom "unpickler" that uses
find_class()
:Then you'd need to use
renamed_load()
instead ofpickle.load()
andrenamed_loads()
instead ofpickle.loads()
.pickle
serializes classes by reference, so if you change were the class lives, it will not unpickle because the class will not be found. If you usedill
instead ofpickle
, then you can serialize classes by reference or directly (by directly serializing the class instead of it's import path). You simulate this pretty easily by just changing the class definition after adump
and before aload
.As pickle's docs say, in order to save and restore a class instance (actually a function, too), you must respect certain constraints:
whyteboard.tools
is not the "the same module as"tools
(even though it can be imported byimport tools
by other modules in the same package, it ends up insys.modules
assys.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...:and then
cPickle.load
each file,del sys.modules['tools']
, andcPickle.dump
each loaded object back to file: that temporary extra entry insys.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).