On this page
pickle
Prev: pdb | Up: stdlib_module | Next: pickletools
Rel: copyreg, shelve, marshal, dbm, sqlite3
Ref: docs.python.org — pickle
Tags: #public #python3 #stdlib
Import
import pickle
Overview
Python-specific binary object serializer (dumps, loads, dump, load) capable of persisting arbitrary Python object graphs (including custom classes and cyclic references). CRITICAL SECURITY RULE: pickle.loads() executes arbitrary code during deserialization—use it only for trusted, same-process/same-machine caches, and use json, sqlite3, or struct for any external or untrusted data.
Quick Start
import pickle
payload = {"users": ["Ada", "Grace"], "scores": (10, 20)}
blob = pickle.dumps(payload, protocol=pickle.HIGHEST_PROTOCOL)
restored = pickle.loads(blob)
assert restored == payload
Implementation Walkthrough
1. Caching Trusted Local Artifacts with Protocol Version Pinning
import pickle
from pathlib import Path
def save_cache(path: Path, obj: object) -> None:
with path.open("wb") as fh:
pickle.dump(obj, fh, protocol=pickle.HIGHEST_PROTOCOL)
def load_cache(path: Path) -> object:
with path.open("rb") as fh:
return pickle.load(fh)
Gotchas & Boundaries
- NEVER unpickle untrusted data: A crafted pickle payload can run arbitrary OS commands (
os.system,subprocess) onpickle.loads(). EvenRestrictedUnpickleris hard to secure completely. - Tightly coupled to your code layout: Renaming or moving a class breaks loading previously pickled instances.
- Open files in binary mode (
"wb"/"rb"): Pickle streams are binary, not UTF-8 text. - Prefer json, sqlite3, or
dataclassescodecs for long-lived storage or cross-language data.
Common Functions
- pickle.dump() —
dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)— Write a pickled representation of obj to the open file object file. - pickle.dumps() —
dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)— Return the pickled representation of the object as a bytes object. - pickle.load() —
load(file, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=())— Read and return an object from the pickle data stored in a file. - pickle.loads() —
loads(data, /, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=())— Read and return an object from the given pickle data.
Classes & Objects
- pickle.PickleBuffer — Wrapper for potentially out-of-band buffers (Methods:
.raw(),.release()) - pickle.PickleError —
pickle.PickleErrorclass. (Methods:.add_note(),.with_traceback()) - pickle.Pickler — This takes a binary file for writing a pickle data stream. (Methods:
.clear_memo(),.dump(),.persistent_id()) - pickle.PicklingError —
pickle.PicklingErrorclass. (Methods:.add_note(),.with_traceback()) - pickle.Unpickler — This takes a binary file for reading a pickle data stream. (Methods:
.find_class(),.load(),.persistent_load()) - pickle.UnpicklingError —
pickle.UnpicklingErrorclass. (Methods:.add_note(),.with_traceback())
Constants & Attributes
pickle.ADDITEMS(bytes) =b'\x90'pickle.APPEND(bytes) =b'a'pickle.APPENDS(bytes) =b'e'pickle.BINBYTES(bytes) =b'B'pickle.BINBYTES8(bytes) =b'\x8e'pickle.BINFLOAT(bytes) =b'G'pickle.BINGET(bytes) =b'h'pickle.BININT(bytes) =b'J'pickle.BININT1(bytes) =b'K'pickle.BININT2(bytes) =b'M'pickle.BINPERSID(bytes) =b'Q'pickle.BINPUT(bytes) =b'q'pickle.BINSTRING(bytes) =b'T'pickle.BINUNICODE(bytes) =b'X'pickle.BINUNICODE8(bytes) =b'\x8d'
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/bz2
- tuts/python3/stdlib_module/compression
- tuts/python3/stdlib_module/copyreg
- tuts/python3/stdlib_module/dbm
- tuts/python3/stdlib_module/gzip
- tuts/python3/stdlib_module/lzma
- tuts/python3/stdlib_module/marshal
- tuts/python3/stdlib_module/pdb
- tuts/python3/stdlib_module/pickle/dump
- tuts/python3/stdlib_module/pickle/dumps
- tuts/python3/stdlib_module/pickle/load
- tuts/python3/stdlib_module/pickle/loads
- tuts/python3/stdlib_module/pickle/PickleBuffer
- tuts/python3/stdlib_module/pickle/PickleError
- tuts/python3/stdlib_module/pickle/Pickler
- tuts/python3/stdlib_module/pickle/PicklingError
- tuts/python3/stdlib_module/pickle/Unpickler
- tuts/python3/stdlib_module/pickle/UnpicklingError
- tuts/python3/stdlib_module/pickletools
- tuts/python3/stdlib_module/shelve
- tuts/python3/stdlib_module/sqlite3
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tarfile
- tuts/python3/stdlib_module/zipfile
- tuts/python3/stdlib_module/zlib
Related notes
- tuts/python3/stdlib_module/copyreg Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/dbm Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/marshal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pdb Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle/dump Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle/dumps Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle/load Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle/loads Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle/PickleBuffer Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle/PickleError Linked here; links to this page; shared tags: #stdlib