To disassemble a Python module, first this has to be turned into a .pyc
file (Python compiled). To do this, run
python -m compileall <file>.py
Then in an interpreter, run
import dis
import marshal
with open("<file>.pyc", "rb") as code_f:
code_f.read(8) # Magic number and modification time
code = marshal.load(code_f) # Returns a code object which can be disassembled
dis.dis(code) # Output the disassembly
This will compile a Python module and output the bytecode instructions with dis
. The module is never imported so it is safe to use with untrusted code.