Introduction
When CPython imports a module from a
.py file, it compiles the source to bytecode
and saves the result as a .pyc file in a __pycache__ folder
beside the source. The next import loads the .pyc file and skips the
compiler. The file holds the module’s code object: its instructions, constants and
names, with the code object of every function and class nested inside.
This page covers the format written by Python 3.11. Python 3.11 rebuilt the code object: local, cell and free variables share one list, a qualified name was added, line numbers became a table that also records columns, and exception handling moved from instructions into a table of its own. Instructions gained inline caches.
The magic number at the start of the file says which release wrote it, and a file only runs on a release with exactly the same number. The final Python 3.11 releases write 3495.
A .pyc file dropped on any page of this site opens in the explorer, which
shows its structure, a disassembly and each code object.
Structure
A file is a 16-byte header followed by one object in Python’s
marshal format, the module’s code object. All numbers are little-endian.
0magic: a release’s number, then0D 0A4 bytes4flags: 0 for a date check, 1 or 3 for a hash check4 bytes8source_mtime and source_size, or source_hash8 bytes16code object of the module, onemarshalobjectvaries
The code object holds counts, flags, the instructions as a byte string, a tuple of constants (which holds nested code objects), and tables of names, source positions and exception handlers.
What Python 3.11 changed
The header is the 16-byte one of Python 3.7 to 3.10. Almost everything after it changed:
- The code object drops
co_nlocals,co_varnames,co_cellvarsandco_freevars. One tuple of names,co_localsplusnames, holds every local, cell and free variable, and a byte string,co_localspluskinds, says what each one is. - A qualified name,
co_qualname, followsco_name. co_linetablebecomes the location table of PEP 657: each instruction has a start and end line and a start and end column.- Exception handlers are no longer set up by instructions. A new field,
co_exceptiontable, lists the ranges of instructions each handler covers. - Some instructions are followed by inline cache entries, which are zero in a file.
- The numeric
BINARY_*andINPLACE_*instructions become oneBINARY_OP.COPYandSWAPreplace theDUP_*andROT_*instructions. Calls usePUSH_NULL,PRECALLandCALL. Every jump is relative, in a forward or a backward form.RESUMEstarts each code object. - Magic numbers follow a rule: Python 3.n starts at 2900 + 50n, so 3.11 starts at 3450.
Every magic number of the 3.11 pre-releases is on the
header page, with the commit that set it
and what it changed in the instruction table. Numbers 3450 to 3458 have earlier forms of
the code object (the fields were added one at a time), 3459 to 3493 (alphas 1 to 7) have
two more tables after the line table, the end lines and the columns
(co_endlinetable, co_columntable), which the location table of
3494 replaced. The viewer reads every one of them, with the instruction table of its own
number. No final release wrote any of them.
Telling it apart
The first two bytes, little-endian, are the magic number: 3450 to 3495, and 3495 in
every final 3.11 release. The next two bytes are 0D 0A. The explorer takes
these numbers and then reads the file strictly: only 0, 1 and 3 are valid flags, and the
code object must fill the rest of the file exactly.
Files of Python 3.10 have a smaller magic number, a code object with
co_nlocals, co_varnames, co_freevars and
co_cellvars, and no exception table. Files of Python 3.12 have the same
layout as 3.11 but a magic number of 3500 or more, and number some instructions
differently.
00000000 A7 0D 0D 0A 00 00 00 00 A0 AC 34 68 00 00 00 00
0x6834ACA0 (26 May 2025), source size 0Not described
The viewer and this reference were checked against a real CPython 3.11 interpreter (3.11.18), with no differences on up to 250 library modules. What remains:
Known, not yet covered
- Viewer: files from the 3.11 alphas and betas (magic 3450 to 3494) are
read with the table and code-object layout of their own number, checked against a real
build of the commit that first set each number (60 library modules each, no
differences); the viewer shows a notice for them. A commit between one number and the
next can differ from the commit that set it, so a file from such a commit can still be
named wrongly. The commit that set 3495 on the 3.11 branch is not the final release:
it can write the quickened
EXTENDED_ARG(opcode 34), which the viewer names. - 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 flags, timestamp and source size (or source hash) were not compared field by field with what the interpreter wrote.
- Reference: other implementations (PyPy, MicroPython’s
.mpy, Cython-built files) are not CPython.pycfiles and are not described.
Variants
- Python 1.5 to 2.0 bytecode – 8-byte header, one or three-byte instructions and 16-bit numbers in the code object
- Python 2.1 and 2.2 bytecode – adds free and cell variables to the code object
- Python 2.3 to 2.7 bytecode – the code object’s numbers become 32-bit
- Python 3.0 to 3.2 bytecode – keyword-only arguments; text and bytes become separate types
- Python 3.3 to 3.5 bytecode – 12-byte header with the source size; object references from 3.4
- Python 3.6 bytecode – two-byte instructions
- Python 3.7 bytecode – 16-byte header with a flags field
- Python 3.8 and 3.9 bytecode – positional-only arguments
- Python 3.10 bytecode –
co_linetable, and jumps that count instructions - Python 3.11 bytecode – one list of locals, 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;
python -m disshows the bytecode (Windows, macOS, Linux) - Decompyle++ (pycdc) – disassembler and decompiler for
.pycfiles of many Python versions (Windows, macOS, Linux)
Developers
- dis – Python’s own bytecode disassembler (Python)
- marshal – Python’s own module for the object format of the body (Python)
- py_compile – Python’s own module that writes
.pycfiles (Python) - importlib – Python’s import system, which reads and checks them (Python)
- xdis – cross-version disassembler (Python)
Reference
- dis – Python’s documentation of its bytecode instructions (Python 3.11)
- marshal – Python’s documentation of the module and its version numbers (Python 3.11)
- importlib – Python’s documentation of the import system (Python 3.11)
- PEP 3147 – the
__pycache__folder and its file names - PEP 552 – the flags field and hash-based
.pycfiles - PEP 626 – precise line numbers for tracing
- PEP 657 – columns in the location table
- locations.md – CPython’s description of the location table (Python 3.11)
- exception_handling_notes.txt – CPython’s description of the exception table (Python 3.11)
- _bootstrap_external.py – the importer, whose comments list every magic number (Python 3.11)