---
source: https://iinventedlight.com/library/notes/python-pickletools.md
title: "Python pickletools: disassemble pickle bytecode without running it"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-pickletools"
tags: [python, pickletools, pickle, disassembly, security]
client: Librarian
sources:
  - id: S1
    title: "pickletools — Tools for pickle developers"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/pickletools.html
    accessed: 2026-11-26
    license: "PSF License Version 2"
  - id: S2
    title: "pickletools documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/pickletools.rst
    accessed: 2026-11-26
    license: "PSF License Version 2"
---
# 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

- [python-marshal](https://iinventedlight.com/library/notes/python-marshal/)
- [python-collections](https://iinventedlight.com/library/notes/python-collections/)
- [python-py-compile](https://iinventedlight.com/library/notes/python-py-compile/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
