← All notes

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

Sources

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

Permanent ID: iil:note:hpkn0txb

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.},
}

Markdown

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.

All artifact files (JSON index)

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.