How NOT to pickle your Python code

I went NUTS trying to figure out why I couldn't unpickle a custom class.

I learned more about modules and packages and classes and dot references in Python than I needed.

And what did the problem boil down to?

One character.

That's right. One. Stinking. Character.

FYI, here's what you need to know about pickling in Python (and by "you", I mean "me").

  • When using binary mode, open both input and output files using 'b'. For example, file(thefilepath, 'wb'), and file(thefilepath, 'rb'). Yes, that was my single character.
  • Use cPickle where possible. The speed increase is enormous.
  • Search the Python site using the keywords of any errors. I found a clue to my issue within moments of searching on "pickle ValueError". Very handy, even if they do use an evil search engine.

Learn from my mistakes. Don't repeat them.


Written by Andrew Ittner in misc on Tue 06 April 2004. Tags: python