Introduction
When CPython imports a module from a
.py file, it compiles it to bytecode and
saves the result as a .pyc file, so the next import can skip compiling.
From Python 3.2 these files go in a __pycache__ folder beside the source,
named for the interpreter that made them, such as
module.cpython-312.pyc. Up to Python 3.4, optimised bytecode went in
.pyo files.
A file is a short header followed by the module’s code object, written with Python’s
marshal module. The header starts with a magic number: two bytes that
change whenever the bytecode changes, then 0D 0A. The bytecode is not
portable between releases, so a .pyc file only runs on the Python that
wrote it, and the magic number says which one that is.
The format has changed many times. Versions that only add new instructions are grouped into one variant; a version that changes the header, the fields of a code object or how instructions and line numbers are stored starts a new one. From Python 3.11 every release numbers its instructions differently, so each release is a variant of its own. This gives fourteen variants. Each has its own complete reference.
A .pyc or .pyo file dropped on any page of this site opens
in the explorer, which shows its structure, a disassembly and each code object.
Variations
| Format | Variation | About | Open |
|---|---|---|---|
.PYC | Python 1.5 to 2.0 bytecode | Python’s bytecode cache, releases 1.5 to 2.0: an 8-byte header and 16-bit code fields. | Open |
.PYC | Python 2.1 and 2.2 bytecode | Python’s bytecode cache, 2.1 and 2.2: 16-bit code fields and nested-scope variables. | Open |
.PYC | Python 2.3 to 2.7 bytecode | Python’s bytecode cache, 2.3 to 2.7: an 8-byte header and 32-bit code fields. | Open |
.PYC | Python 3.0 to 3.2 bytecode | Python’s bytecode cache, 3.0 to 3.2: 8-byte header, keyword-only arguments, Unicode names. | Open |
.PYC | Python 3.3 to 3.5 bytecode | CPython 3.3 to 3.5 files: 12-byte header, one-byte and three-byte instructions. | Open |
.PYC | Python 3.6 bytecode | CPython 3.6 files: 12-byte header, two-byte instructions, signed line steps. | Open |
.PYC | Python 3.7 bytecode | CPython 3.7 files: 16-byte header with flags and an optional source hash. | Open |
.PYC | Python 3.8 and 3.9 bytecode | CPython 3.8 and 3.9 files: 16-byte header, positional-only argument count. | Open |
.PYC | Python 3.10 bytecode | co_linetable line numbers and jumps that count instructions: magic 3430 to 3439. | Open |
.PYC | Python 3.11 bytecode | One list of locals, location and exception tables, inline caches: magic 3450 to 3495. | Open |
.PYC | Python 3.12 bytecode | Inlined comprehensions, LOAD_ATTR method loads, forward-only jumps: magic 3500 to 3531. | Open |
.PYC | Python 3.13 bytecode | Renumbered instructions, explicit bool conversion, caches on jumps: magic 3550 to 3571. | Open |
.PYC | Python 3.14 bytecode | Marshal version 5 with slice constants, LOAD_SMALL_INT: magic 3600 to 3627. | Open |
.PYC | Python 3.15 bytecode | Marshal version 6 with frozen dictionaries, lazy imports: magic 3650 to 3666. | Open |
Python 1.5 to 2.0 bytecode
The oldest format covered here. The header is 8 bytes: the magic number and the source’s modification time. Numbers in a code object are 16-bit, and there are no free or cell variables. An instruction is one byte, or three when it has an argument.
The magic number is 20121 (1.5), 50428 (1.6) or 50823 (2.0). With Python’s
-U option, a 2.x file uses the magic number plus one.
Python 2.1 and 2.2 bytecode
The code object gains co_freevars and co_cellvars for
nested scopes. Numbers are still 16-bit. The header is 8 bytes.
The magic number is 60202 (2.1) or 60717 (2.2), or one more with
-U.
Python 2.3 to 2.7 bytecode
The numbers in a code object become 32-bit. From 2.4, marshal can save strings to
a list and refer back to them. The header is 8 bytes, and .pyo files
hold optimised code.
The magic number is between 62011 and 62211, or one more with -U.
Python 3.0 to 3.2 bytecode
Text and bytes become separate types, and code objects gain
co_kwonlyargcount. The header is 8 bytes.
The magic number is between 3000 and 3180.
Python 3.3 to 3.5 bytecode
The header grows to 12 bytes with the size of the source. From 3.4 marshal can mark any object to be referred to again. From 3.5 file names carry an optimisation level.
The magic number is between 3190 and 3351.
Python 3.6 bytecode
Instructions become two bytes each, one of opcode and one of argument, and line
steps in co_lnotab become signed. The header is 12 bytes.
The magic number is between 3360 and 3379.
Python 3.7 bytecode
The header grows to 16 bytes with a flags field. The file can be checked by a hash of the source instead of its date and size (PEP 552).
The magic number is between 3390 and 3394.
Python 3.8 and 3.9 bytecode
The code object gains co_posonlyargcount.
The magic number is between 3400 and 3425.
Python 3.10 bytecode
co_linetable replaces co_lnotab, and a jump’s argument
counts instructions rather than bytes.
The magic number is between 3430 and 3439.
Python 3.11 bytecode
Python 3.11 rebuilt the code object: locals, cells and free variables share one list,
and it gains a qualified name, a location table with columns and an exception table.
Some instructions are followed by inline caches. Calls use PRECALL and
CALL, and jumps come in forward and backward forms. Every later release
keeps this file layout.
The magic number is between 3450 and 3495.
Python 3.12 bytecode
List, dictionary and set comprehensions are compiled inline, which adds a hidden kind
of local. LOAD_METHOD is folded into LOAD_ATTR,
PRECALL is gone, and conditional jumps only go forward. Rare operations
move to CALL_INTRINSIC_1 and CALL_INTRINSIC_2.
The magic number is between 3500 and 3531.
Python 3.13 bytecode
Every instruction is renumbered, in alphabetical blocks. Conversion to
bool becomes a separate instruction, TO_BOOL, and
conditional jumps get an inline cache. CALL_KW replaces
KW_NAMES, and SET_FUNCTION_ATTRIBUTE takes over the
arguments of MAKE_FUNCTION.
The magic number is between 3550 and 3571.
Python 3.14 bytecode
marshal version 5 adds slice constants. BINARY_SUBSCR
becomes a BINARY_OP, small integers load with
LOAD_SMALL_INT, and template strings get their own instructions.
co_flags gains HAS_DOCSTRING and METHOD, and
co_localspluskinds gains bits for the kinds of parameter.
The magic number is between 3600 and 3627.
Python 3.15 bytecode
marshal version 6 adds frozen dictionaries. RESUME and
GET_ITER gain inline caches, and IMPORT_NAME’s argument
carries lazy-import settings.
The magic number is between 3650 and 3666.
Versions
The magic number is the file’s first two bytes, little-endian, followed by
0D 0A. The table gives each release’s final magic number. Pre-releases used
others, and each variant’s header page lists them.
| Python | Magic | Header size | Variant |
|---|---|---|---|
| 1.5 | 20121 | 8 | Python 1.5 to 2.0 bytecode |
| 1.6 | 50428 | 8 | Python 1.5 to 2.0 bytecode |
| 2.0 | 50823 | 8 | Python 1.5 to 2.0 bytecode |
| 2.1 | 60202 | 8 | Python 2.1 and 2.2 bytecode |
| 2.2 | 60717 | 8 | Python 2.1 and 2.2 bytecode |
| 2.3 | 62011 | 8 | Python 2.3 to 2.7 bytecode |
| 2.4 | 62061 | 8 | Python 2.3 to 2.7 bytecode |
| 2.5 | 62131 | 8 | Python 2.3 to 2.7 bytecode |
| 2.6 | 62161 | 8 | Python 2.3 to 2.7 bytecode |
| 2.7 | 62211 | 8 | Python 2.3 to 2.7 bytecode |
| 3.0 | 3131 | 8 | Python 3.0 to 3.2 bytecode |
| 3.1 | 3151 | 8 | Python 3.0 to 3.2 bytecode |
| 3.2 | 3180 | 8 | Python 3.0 to 3.2 bytecode |
| 3.3 | 3230 | 12 | Python 3.3 to 3.5 bytecode |
| 3.4 | 3310 | 12 | Python 3.3 to 3.5 bytecode |
| 3.5 | 3350; 3351 from 3.5.3 | 12 | Python 3.3 to 3.5 bytecode |
| 3.6 | 3379 | 12 | Python 3.6 bytecode |
| 3.7 | 3394 | 16 | Python 3.7 bytecode |
| 3.8 | 3413 | 16 | Python 3.8 and 3.9 bytecode |
| 3.9 | 3425 | 16 | Python 3.8 and 3.9 bytecode |
| 3.10 | 3439 | 16 | Python 3.10 bytecode |
| 3.11 | 3495 | 16 | Python 3.11 bytecode |
| 3.12 | 3531 | 16 | Python 3.12 bytecode |
| 3.13 | 3571 | 16 | Python 3.13 bytecode |
| 3.14 | 3627 | 16 | Python 3.14 bytecode |
| 3.15 | 3666 | 16 | Python 3.15 bytecode |
A number between the releases above belongs to a pre-release of the next one. Python 1.5.1 and 1.5.2 share 20121, and Python 2.0.1 and 2.1.2 keep their release’s number.
Before 3.7, there is no flags field, and the source’s modification time follows the magic number. The 3.3 to 3.6 headers add the source’s size. From 3.7 a file can be checked by a hash of the source instead.
Not described
The viewer and this reference were checked against real CPython interpreters for every released version from 1.5 to 3.15 (1.5.2, 1.6 from the 1.6 tarball, 2.0, 2.1.3, 2.2.3, 2.3.7, 2.4.6, 2.5.6, 2.6.9, 2.7, 3.0.1, 3.1.5, 3.2.6, 3.3.7, 3.4.10, 3.5, 3.6, 3.7, 3.8.20, 3.9, 3.10, 3.11.18, 3.12, 3.13, 3.14 and 3.15.2), with no differences on up to 250 library modules each. What remains is named on each variant’s own page; in short:
Known, not yet covered
- Viewer: files from the pre-releases of Python 3.11 to 3.15 (every magic number from 3450 to 3665 that a commit ever set) are read with the opcode table and code-object layout of their own number, each checked against a real build of the commit that first set the number (60 library modules, no differences; the numbers and commits are on each release’s header page); the viewer shows a notice for them. A commit between one magic number and the next can differ from the commit that set it, and the number 3571 is also that of a short-lived main-branch commit of 3.14 (see the 3.13 header page). The pre-releases of 3.10 and earlier, and the numbers above 3666, are read with the final release’s opcode table, so an instruction name can be wrong; the viewer shows a notice for them.
- Viewer: only the opcodes that appear in the test modules are checked as live instructions; the rest are checked against the opcode table.
- Reference: header flags, timestamp and size values were not compared field by field with what the interpreters wrote.
- Reference: non-CPython implementations (PyPy, Jython, MicroPython’s
.mpy, Cython-built files) are not.pycfiles and are not described, except where a page already covers them.
Links
Software
- CPython – the Python interpreter, which writes and loads these files; its
dismodule shows the bytecode (Windows, macOS, Linux) - Decompyle++ (pycdc) – disassembler and decompiler for
.pycfiles of many Python versions (Windows, macOS, Linux) - uncompyle6 – decompiler for Python versions up to 3.8 (any system with Python)
- decompyle3 – decompiler for Python 3.7 and 3.8 (any system with Python)
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)