views:

105

answers:

2

Hello,

I've got a simple class that I am pickling(dumping) to a file. On OS X this works fine, and on Windows this works fine.

However, while on windows I can load/unpickle the object fine - when windows then pickles this file and saves it back to disk, it becomes unreadable on OS X (although in Windows it still behaves as normal).

The error I get back from OS X is that it is unable to import the require class.

I'm confused as this all works fine as long as I don't pickle anything in windows! (Even then it still works fine in Windows)

I've heard it could be line endings, my other thoughts are possibly something to do with the encoding type used being different across operating systems? But I really have no idea what to try to fully diagnose and/or solve this problem, so any help would be appreciated!

+3  A: 

Pickle with the newest protocol version and open the files in binary mode in all cases. That should solve the problem.

Cory Petosky
Thanks, this worked perfectly
Lee
+2  A: 

It will be line endings - if you are using ASCII pickle open file in ascii mode 'r' or 'w' - if you are using a binary pickle open in binary mode 'rb' 'wb'. From the docstring:

The default protocol is 0, to be backwards compatible. (Protocol 0 is the only protocol that can be written to a file opened in text mode and read back successfully. When using a protocol higher than 0, make sure the file is opened in binary mode, both when pickling and unpickling.)

thrope