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. Marshal adds u, Unicode strings. Three new instructions for the extended call syntax f(*args, **kwargs): CALL_FUNCTION_VAR, CALL_FUNCTION_KW and CALL_FUNCTION_VAR_KW |
| 2.0 | 50823, or 50824 with -U | New instructions: in-place operators, print >>, IMPORT_STAR, ROT_FOUR, DUP_TOPX and EXTENDED_ARG. UNPACK_SEQUENCE replaces the two older unpack instructions, and IMPORT_FROM now pushes the value it imports |
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.
Not described
The viewer and this reference were checked against real CPython interpreters for Python 1.5.2, 1.6 (built from the 1.6 tarball, not 1.6.1) and 2.0, with no differences on up to 250 library modules each. What remains:
Known, not yet covered
- Viewer: files from pre-release builds (alpha, beta and candidate magic numbers) are read with the final release’s opcode table, so an instruction name can be wrong; the viewer shows a notice for them.
- Viewer: the interpreter comparison only exercised the opcodes that appear in the test modules as live instructions; the rest are checked against the opcode table.
- Reference: the header’s timestamp was not compared field by field with what the interpreters wrote.
- Reference: other implementations (Jython, for instance) are not
.pycfiles in this sense and are not described.
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 bytecode – locals in one list, a location table, an exception table and inline caches
- Python 3.12 bytecode – inlined comprehensions, method loads folded into
LOAD_ATTR, forward-only conditional jumps - Python 3.13 bytecode – instructions renumbered, explicit conversion to
bool, caches on jumps - Python 3.14 bytecode –
marshalversion 5 with slice constants, small integers in the argument - Python 3.15 bytecode –
marshalversion 6, lazy imports and a cache onRESUME
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