Python pickletools: disassemble pickle bytecode without running it
Answer: This module holds constants about the intimate details of pickle, plus a few useful functions for analyzing pickled data.
Who it is for
Python core developers working on pickle will find this module's contents useful.
Ordinary users of the pickle module probably won't find the pickletools module relevant.
The RST synopsis says it contains extensive comments about the pickle protocols and pickle-machine opcodes.
Command line and helpers
Command-line usage was added in version 3.2.
Invoked as python -m pickletools, it will disassemble the contents of one or more pickle files.
To see the Python object stored in the pickle rather than the pickle format details, you may want to use -m pickle instead.
-m pickletools is safer for an untrusted pickle file, because it does not execute pickle bytecode.
dis() outputs a symbolic disassembly of the pickle to a file-like object, defaulting to sys.stdout. pickle can be a string or a file-like object.
genops() iterates every opcode in a pickle as an (opcode, arg, pos) triple.
optimize() returns a shorter equivalent pickle string after unused PUT opcodes are eliminated, which requires less storage space.
Version 3.15 added color output by default, controlled using environment variables.
See also
Claims
- id: C1 claim: The command-line interface was added in 3.2. source: S1 evidence: "Added in version 3.2."
- id: C2 claim: The safer path does not execute pickle bytecode. source: S1 evidence: "it does not execute pickle bytecode."
- id: C3 claim: optimize() builds a shorter equivalent pickle string by dropping unused PUT opcodes. source: S2 evidence: "Returns a new equivalent pickle string after eliminating unused `
PUT` opcodes."
Sources
- [S1] pickletools — Tools for pickle developers. Python Software Foundation. https://docs.python.org/3/library/pickletools.html . License: PSF License Version 2. Accessed 2026-11-26.
- [S2] pickletools documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/pickletools.rst . License: PSF License Version 2. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, pickletools — Tools for pickle developers; CPython, pickletools documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, pickletools — Tools for pickle developers; CPython, pickletools documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python pickletools: disassemble pickle bytecode without running it." iinventedlight Library, iil:note:hpkn0txb, version of 2026-11-26. https://iinventedlight.com/library/notes/python-pickletools/. License: CC-BY-4.0.
@misc{iil_note_hpkn0txb,
author = {{iinventedlight}},
title = {{Python pickletools: disassemble pickle bytecode without running it}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-pickletools/},
note = {Permanent ID iil:note:hpkn0txb. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-pickletools.schema.json (schema; CC-BY-4.0)
- python-pickletools.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-pickletools.md.