Introduction
A .pyc file of this format holds one Python module compiled by
CPython 3.6, so that the next import can skip
compiling it. Python writes it to a __pycache__ folder beside the source,
named module.cpython-36.pyc, and only CPython 3.6 will load it.
The file is a 12-byte header followed by the module’s code object, written in
Python’s marshal format. The code object holds the instructions as
two-byte pairs, its constants and names, and a table that maps instructions to source
lines. Functions and classes defined in the module are further code objects inside its
constants.
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
All numbers are little-endian.
0magic: a 16-bit number, then0D 0A4 bytes4source_mtime: the source’s modification time4 bytes8source_size: the source’s size in bytes4 bytes12code object of the module, in marshal formatvaries
Every code object holds its
instructions in
co_code and its line
numbers in co_lnotab.
What this release changed
Python 3.6 changed how instructions and line numbers are stored. The numbers below come from the magic number list in CPython’s source; the full list is on the Header page.
| Magic | Change |
|---|---|
| 3360 | Added FORMAT_VALUE, for f-strings |
| 3361 | The line step in co_lnotab became signed |
| 3370 | Every instruction became two bytes |
| 3371, 3373 | Added BUILD_CONST_KEY_MAP and BUILD_STRING |
| 3372 | MAKE_FUNCTION simplified, MAKE_CLOSURE removed |
| 3375 | Added SETUP_ANNOTATIONS and STORE_ANNOTATION |
| 3376 | The CALL_FUNCTION family and BUILD_MAP_UNPACK_WITH_CALL simplified |
| 3378 | Added BUILD_TUPLE_UNPACK_WITH_CALL |
Telling it apart
The first two bytes, read as a little-endian number, are the magic number, and they
must be followed by 0D 0A. The explorer treats 3360 to 3389 as this
format: a final 3.6 file starts 33 0D 0D 0A. The magic number settles it
alone. Below 3360 instructions take one or three bytes, and from 3392 the header is 16
bytes.
Files with magic 3360 or 3361, from before two-byte instructions went in at 3370, have
one-byte and three-byte instructions, and the explorer splits them that way. It names
opcodes from the final 3.6 table for every file, so in pre-release files from before
3376 MAKE_CLOSURE (134) and CALL_FUNCTION_VAR (140) show by
number, and CALL_FUNCTION_VAR_KW (142) shows as
CALL_FUNCTION_EX.
Not described
The viewer and this reference were checked against a real CPython 3.6 interpreter, with no differences on up to 250 library modules. What remains:
Known, not yet covered
- Viewer: files from 3.6 pre-releases (alpha, beta and candidate magic numbers) are read with the final 3.6 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 and source size 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 – 16-bit numbers in a code object, an 8-byte header
- Python 2.1 and 2.2 bytecode – adds cell and free variables
- Python 2.3 to 2.7 bytecode – the numbers become 32-bit
- Python 3.0 to 3.2 bytecode – adds
co_kwonlyargcount; names are Unicode - Python 3.3 to 3.5 bytecode – a 12-byte header with the source size
- Python 3.6 bytecode – two-byte instructions
- Python 3.7 bytecode – a 16-byte header with flags and hash-based files
- Python 3.8 and 3.9 bytecode – adds
co_posonlyargcount - Python 3.10 bytecode –
co_linetablereplacesco_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 (Windows, macOS, Linux)
- uncompyle6 – decompiler for Python bytecode up to 3.8 (any system with Python)
- Decompyle++ (pycdc) – decompiler and disassembler for .pyc files (Windows, macOS, Linux)
Developers
- dis – disassembler for Python bytecode (Python)
- marshal – reads and writes the object format used in the file (Python)
- py_compile – compiles a source file and writes its .pyc file (Python)
- importlib – Python’s import system, including the header checks (Python)
- xdis – disassembler that reads bytecode from many Python versions (Python)
Reference
- Python Bytecode Instructions – the instruction list in the 3.6
disdocumentation - PEP 3147 – the
__pycache__folder and its file names