21

I have created a small python script of mine. I saved the pickle file on Linux and then used it on windows and then again used it back on Linux but now that file is not working on Linux but it is working perfectly on windows. Is is so that python is coss-platform but the pickle file is not. Is there any solution to this one???

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
vikithakar
  • 213
  • 1
  • 2
  • 4
  • 47
    Python pickle possibly prickly. Pickle platform problems potentially preventable. Present possible problem prevention proposal, please. – aehiilrs Dec 04 '09 at 23:48

6 Answers6

37

Python's pickle is perfectly cross-platform.

This is likely due to EOL (End-Of-Line) differences between Windows and Linux. Make sure to open your pickle files in binary mode both when writing them and when reading them, using open()'s "wb" and "rb" modes respectively.

Note: Passing pickles between different versions of Python can cause trouble, so try to have the same version on both platforms.

taleinat
  • 8,441
  • 1
  • 30
  • 44
  • `pickle` is [guaranteed to be "backwards compatible"](http://docs.python.org/2/library/pickle.html#relationship-to-other-python-modules). I assume this means that newer versions will read pickles from older versions, but older versions may not be able to read pickles from newer versions. – max Dec 28 '12 at 18:12
  • 1
    Note that while the pickle *serialization format* is guaranteed to be backwards compatible across Python releases, other things in Python are not. For example, functions and classes may be added or removed and modules may be renamed. Pickle stores references to functions and classes using their name and module, and such references may break between different versions of Python. So it isn't *generally* safe to load pickles saved with a different version of Python. – taleinat Jul 09 '19 at 09:59
12

The pickle module supports several different data formats. If you are specifying a particular pickle format instead of using the default (0), you may be running into cross-platform binary file problems. You can use plain ASCII pickle files by specifying protocol 0.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
4

Maybe you don't open the file in binary mode? See this stackoverflow question

Community
  • 1
  • 1
catchmeifyoutry
  • 7,179
  • 1
  • 29
  • 26
4

Pickle should be cross-platform, there are versioning/protocol issues, (see http://docs.python.org/library/pickle.html#data-stream-format) but in general if you're using the same release of python on your windows and unix boxes, they should be interoperable.

If you're using pickle as a data transport mechanism, you might want to consider less-implementation specific formats for data storage, such as json, xml, csv, yaml, etc.

ironchefpython
  • 3,409
  • 1
  • 19
  • 32
1

You could use json instead of pickle. If it can save your data, you know it's cross platform.

steveha
  • 74,789
  • 21
  • 92
  • 117
0

One interesting idea to try out is PyON (Python Object Notation). The current version seems to work at least for simple cases according to my tests. There seems to have been some disagreement on mailing lists whether the project is a good idea, though.

akaihola
  • 26,309
  • 7
  • 59
  • 69