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

  1. Import pickle.
  2. Dump an object to a file or bytes with pickle.dump() or pickle.dumps().
  3. Load it back with pickle.load() or pickle.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_PROTOCOL for efficiency and smaller file sizes.
  • For custom classes, ensure the class is importable in the environment where you unpickle.
  • Alternatives like dill and cloudpickle support 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.