Disassembling modules

suggest change

To disassemble a Python module, first turn it into a .pyc file. To do this, run

python -m compileall -b <file>.py

compileall -b writes <file>.pyc beside the source. Without -b, the file is placed under __pycache__ and its name includes the interpreter tag.

Then in an interpreter, run

import dis
import marshal
with open("<file>.pyc", "rb") as code_f:
    code_f.read(16) # 16-byte header since Python 3.7
    code = marshal.load(code_f) # Returns a code object which can be disassembled
    dis.dis(code) # Output the disassembly

This compiles a Python module and prints its bytecode. The module is not imported, so this does not run it. marshal is not safe on untrusted bytes; only load a file you just compiled.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents