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???
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.
pickle
is guaranteed to be "backwards compatible". 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. –
Tiernan 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.
Maybe you don't open the file in binary mode? See this stackoverflow question
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.
You could use json
instead of pickle
. If it can save your data, you know it's cross platform.
pickle
. –
Textual 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.
When Pickle pickles an object, it stores the module and method used to create it and the arguments to feed to that callable.
>>> import pathlib
>>> import pickle
>>> import pickletools
>>> myPath = pickle.dumps(pathlib.Path.home())
>>> pickletools.dis(pickletools.optimize(myPath))
0: \x80 PROTO 3
2: c GLOBAL 'pathlib WindowsPath'
23: X BINUNICODE 'C:\\'
31: X BINUNICODE 'Users'
41: X BINUNICODE '<your_username>'
59: \x87 TUPLE3
60: R REDUCE
61: . STOP
highest protocol among opcodes = 2
Conceptually, unpickling this just does:
from pathlib import WindowsPath
return WindowsPath('C:\\', 'Users', '<your_username>')
Several classes and methods are, under the hood, different between Windows and Linux, or have different behaviour between the two systems.
- pathlib.Path() will create WindowsPath or PosixPath depending on environment.
- os.path will actually be
ntpath
on Windows andposixpath
on Linux.
Likewise, if you have different modules (or different versions of modules) accessible between systems, or the constructor of your object changes, that will make the pickle incompatible.
Likewise, in general you must load and dump pickle files from the same main. If you run two separate scripts
serialize_a.py:
class A:
def __init__(self, num):
self.num = num
if __name__ == "__main__":
import pickle
with open('pickle.pkl', 'wb') as f:
pickle.dump(A(42), f)
import pickle
import serialize_a
if __name__ == "__main__":
with open('pickle.pkl', 'rb') as f:
pickle.load(f)
You'll get an error because the pickle stores A
as being in __main__
, but now the code needs to find it as serialize_a.A()
:
AttributeError: Can't get attribute 'A' on <module '__main__' from ...>
© 2022 - 2025 — McMap. All rights reserved.