Introduction
A .pyc file holds a Python module compiled to bytecode, so that the next
import can skip compiling it. CPython 1.5, 1.6
and 2.0 write one beside each .py file they import: foo.py
gives foo.pyc, or foo.pyo when Python runs with the
-O option.
This page covers the layout those three releases share: an 8-byte header, then one
code object in Python’s marshal format, with every number in the code
object stored in 16 bits. Bytecode changes between releases, so a file only runs on
the release that wrote it. The magic number at the start says which that is.
A .pyc file dropped on any page of this site opens in the explorer,
which shows its structure, a disassembly and each code object.
See all the .pyc variations.
Structure
All numbers are little-endian. The header is 8 bytes and the rest of the file is one object, the module’s code object. Nothing follows it.
0magic: a 16-bit number, then0D 0A4 bytes4source_mtime: the modification time of the source file4 bytes8code object, in marshal formatvaries
The code object holds the module’s instructions, its constants and names, and a table of line numbers. Functions and classes defined in the module are code objects inside its constants.
What each release changed
| Release | Magic number | Changes |
|---|---|---|
| 1.5 | 20121 | The layout on this page. Marshal has the types 0 N . i I f x l s ( [ { c. A statement that unpacks uses UNPACK_TUPLE or UNPACK_LIST |
| 1.6 | 50428 | The same file layout. CPython’s repository has no final 1.6 tag, so this site has no instruction table for it, and the explorer shows a 1.6 file’s opcodes by number |
| 2.0 | 50823, or 50824 with -U | Marshal adds u, Unicode strings. New instructions: in-place operators, print >>, IMPORT_STAR, ROT_FOUR, DUP_TOPX, the extended call forms and EXTENDED_ARG. UNPACK_SEQUENCE replaces the two older unpack instructions |
Telling it apart
Only the first two bytes matter. Read as a little-endian number they are 20121, 50428,
50823 or 50824, and the next two bytes are 0D 0A. The explorer goes by
these numbers alone: the code object has no version field of its own.
The nearest other layout is Python 2.1 and 2.2. Its numbers are also 16-bit, but its
magic numbers are different and its code objects have two more fields,
co_freevars and co_cellvars.
Variants
- Python 1.5 to 2.0 bytecode – 8-byte header and 16-bit numbers in the code object
- Python 2.1 and 2.2 bytecode – adds co_freevars and co_cellvars
- Python 2.3 to 2.7 bytecode – the numbers in a code object become 32-bit
- Python 3.0 to 3.2 bytecode – adds co_kwonlyargcount; names are Unicode
- Python 3.3 to 3.5 bytecode – 12-byte header, with the source size
- Python 3.6 bytecode – two-byte instructions
- Python 3.7 bytecode – 16-byte header, with flags and hash-based files
- Python 3.8 and 3.9 bytecode – adds co_posonlyargcount
- Python 3.10 bytecode – a line table replaces co_lnotab; jumps count instructions
- Python 3.11 and later bytecode – locals in one list, a location table, an exception table and inline caches
Links
Software
- CPython – the Python interpreter, which writes and loads these files
- uncompyle6 – decompiler that reads old .pyc files (any system with Python)
- Decompyle++ (pycdc) – decompiler and disassembler for .pyc files (Windows, macOS, Linux)
Developers
Reference
- Python Byte Code Instructions (1.5.2) – the instructions as documented for Python 1.5.2
- Python Byte Code Instructions (2.0.1) – the same, for Python 2.0.1
- marshal (1.5.2) – the object format as documented for Python 1.5.2