How do I serialize Python objects with pickle and alternatives?
· Category: Python Programming
Short answer
The pickle module serializes almost any Python object to bytes, which can be saved to disk or sent over a network. For safety and interoperability, prefer json for simple data; use pickle only with trusted data.
Steps
- Import
pickle. - Dump an object to a file or bytes with
pickle.dump()orpickle.dumps(). - Load it back with
pickle.load()orpickle.loads().
import pickle
data = {"users": ["alice", "bob"], "count": 42}
# To bytes
serialized = pickle.dumps(data)
restored = pickle.loads(serialized)
print(restored)
# To file
with open("data.pkl", "wb") as f:
pickle.dump(data, f)
with open("data.pkl", "rb") as f:
loaded = pickle.load(f)
print(loaded)
Tips
- Use protocol
pickle.HIGHEST_PROTOCOLfor efficiency and smaller file sizes. - For custom classes, ensure the class is importable in the environment where you unpickle.
- Alternatives like
dillandcloudpicklesupport more types, including lambdas and nested functions.
Common issues
- Unpickling data from untrusted sources is a major security risk because arbitrary code can be executed.
- Pickle protocols are not guaranteed to be compatible across Python versions.
- Objects with open file handles, sockets, or database connections cannot be pickled directly.