Every .pyc file of this format starts with the same 16 bytes of header,
all little-endian. The header says which release wrote the file and how to tell whether
it is out of date. The module’s code object follows at offset 16.
0magic: the number, then0D 0A4 bytes4flags4 bytes8source_mtime, or the first half of source_hash4 bytes12source_size, or the second half of source_hash4 bytes
Magic number
The first two bytes are a 16-bit number, and the next two are always
0D 0A, a carriage return and a line feed. A file copied in text mode has
those bytes changed, so the magic number no longer matches and the file is rejected.
CPython changes the number whenever the bytecode changes in a way an older interpreter cannot run. Python 3.n starts at 2900 + 50n, and the number goes up by one for each change within that release. A file loads only on a release with exactly the same number. The file does not say which release it came from in any other way.
Every final Python 3.11 release writes 3495, the bytes A7 0D 0D 0A. The
numbers 3450 to 3494 come from the alphas and betas; the full list follows
further down.
Flags
A 32-bit field that says how the file is checked against its source. Two bits are defined by PEP 552, and CPython rejects a file with any other bit set.
| Bit | Meaning |
|---|---|
| 0 | Set: the next 8 bytes are a hash of the source. Clear: they are the source’s date and size. |
| 1 | Only with bit 0: the hash is checked against the source when the file is loaded. |
| Value | Kind of file |
|---|---|
| 0 | Checked by the source’s date and size. The usual kind. |
| 1 | Hash-based, not checked when loaded. The file is used even if the source has changed. |
| 3 | Hash-based, checked when loaded. The source is hashed and the file is used only if the hash matches. |
The value 2, bit 1 alone, is never written. CPython’s importer would read such a file
as one checked by date and size, since bit 0 is clear; the explorer turns it down.
py_compile and compileall make hash-based files with their
invalidation mode options. The interpreter’s --check-hash-based-pycs
option (default, always or never) can override
the check when a hash-based file is loaded.
source_mtime
Unsigned 32-bit, at offset 8, when flags is 0. The source file’s modification time in seconds since 1970, as a Unix timestamp, kept to 32 bits. A file is stale if this differs from the source file’s current time.
source_size
Unsigned 32-bit, at offset 12, when flags is 0. The size of the source file in bytes, modulo 232. A file is stale if this differs from the source’s current size.
source_hash
8 bytes at offset 8, when bit 0 of flags is set. A 64-bit SipHash of the source file’s
bytes, keyed by the magic number. importlib.util.source_hash() computes
it. Python’s importer does not recompute it when flags is 1, unless
--check-hash-based-pycs always is given.
Examples
00000000 A7 0D 0D 0A 00 00 00 00 A0 AC 34 68 00 00 00 00
__init__.py, checked by date: magic 3495, flags 0, modification time 0x6834ACA0 (1748282528, 26 May 2025), source size 0 bytesA hash-based file starts A7 0D 0D 0A followed by 01 00 00 00
or 03 00 00 00, and then the eight bytes of the hash.
File names and folders
importlib stores a module’s compiled form in a __pycache__
folder beside the source, named with the interpreter’s tag, as PEP 3147 describes.
The tag gives the release, so several versions of Python can share one source folder.
| Source | Compiled |
|---|---|
spam.py, Python 3.11, no optimisation | __pycache__/spam.cpython-311.pyc |
spam.py, Python 3.11 with -O | __pycache__/spam.cpython-311.opt-1.pyc |
spam.py, Python 3.11 with -OO | __pycache__/spam.cpython-311.opt-2.pyc |
A spam.pyc file with no spam.py beside it is loaded as a
module that has no source. The .pyo extension is not used in this format.
All magic numbers
The list is the one kept in Python 3.11’s own source, in the comments of
Lib/importlib/_bootstrap_external.py. The number marked final is the one
every released 3.11 version writes. The others come from alphas and betas, and the
release shown is the pre-release that introduced them.
| Magic | Release | What changed |
|---|---|---|
| 3450 | 3.11a1 | Use the exception table for unwinding (“zero cost” exception handling) |
| 3451 | 3.11a1 | Add CALL_METHOD_KW |
| 3452 | 3.11a1 | Drop nlocals from marshalled code objects |
| 3453 | 3.11a1 | Add co_fastlocalnames and co_fastlocalkinds |
| 3454 | 3.11a1 | Compute cell offsets relative to locals |
| 3455 | 3.11a1 | Add MAKE_CELL |
| 3456 | 3.11a1 | Interleave cell arguments |
| 3457 | 3.11a1 | Change localsplus to a bytes object |
| 3458 | 3.11a1 | Imported objects no longer use LOAD_METHOD and CALL_METHOD |
| 3459 | 3.11a1 | PEP 657: add end line numbers and column offsets for instructions |
| 3460 | 3.11a1 | Add the co_qualname field |
| 3461 | 3.11a1 | JUMP_ABSOLUTE must jump backwards |
| 3462 | 3.11a2 | Remove COPY_DICT_WITHOUT_KEYS; change MATCH_CLASS and MATCH_KEYS; add COPY |
| 3463 | 3.11a3 | JUMP_IF_NOT_EXC_MATCH no longer pops the active exception |
| 3464 | 3.11a3 | Merge the numeric BINARY_* and INPLACE_* instructions into BINARY_OP |
| 3465 | 3.11a3 | Add COPY_FREE_VARS |
| 3466 | 3.11a4 | PEP 654: except* |
| 3467 | 3.11a4 | Change the CALL_xxx instructions |
| 3468 | 3.11a4 | Add SEND |
| 3469 | 3.11a4 | Remove type and traceback from exc_info |
| 3470 | 3.11a4 | PREP_RERAISE_STAR no longer pushes lasti |
| 3471 | 3.11a4 | Remove POP_EXCEPT_AND_RERAISE |
| 3472 | 3.11a4 | Replace GEN_START with POP_TOP |
| 3473 | 3.11a4 | Add POP_JUMP_IF_NOT_NONE and POP_JUMP_IF_NONE |
| 3474 | 3.11a4 | Add RESUME |
| 3475 | 3.11a5 | Add RETURN_GENERATOR |
| 3476 | 3.11a5 | Add ASYNC_GEN_WRAP |
| 3477 | 3.11a5 | Replace DUP_TOP and DUP_TOP_TWO with COPY; ROT_TWO, ROT_THREE, ROT_FOUR and ROT_N with SWAP |
| 3478 | 3.11a5 | New CALL instructions |
| 3479 | 3.11a5 | Add PUSH_NULL |
| 3480 | 3.11a5 | New CALL instructions, second iteration |
| 3481 | 3.11a5 | Use an inline cache for BINARY_OP |
| 3482 | 3.11a5 | Use inline caching for UNPACK_SEQUENCE and LOAD_GLOBAL |
| 3483 | 3.11a5 | Use inline caching for COMPARE_OP and BINARY_SUBSCR |
| 3484 | 3.11a5 | Use inline caching for LOAD_ATTR, LOAD_METHOD and STORE_ATTR |
| 3485 | 3.11a5 | Add an argument to GET_AWAITABLE |
| 3486 | 3.11a6 | Use inline caching for PRECALL and CALL |
| 3487 | 3.11a6 | Remove the adaptive “oparg counter” mechanism |
| 3488 | 3.11a6 | LOAD_GLOBAL can push an additional NULL |
| 3489 | 3.11a6 | Add JUMP_BACKWARD, remove JUMP_ABSOLUTE |
| 3490 | 3.11a6 | Remove JUMP_IF_NOT_EXC_MATCH, add CHECK_EXC_MATCH |
| 3491 | 3.11a6 | Remove JUMP_IF_NOT_EG_MATCH, add CHECK_EG_MATCH and JUMP_BACKWARD_NO_INTERRUPT, make JUMP_NO_INTERRUPT virtual |
| 3492 | 3.11a7 | Make POP_JUMP_IF_NONE, NOT_NONE, TRUE and FALSE relative |
| 3493 | 3.11a7 | Make JUMP_IF_TRUE_OR_POP and JUMP_IF_FALSE_OR_POP relative |
| 3494 | 3.11a7 | New location info table |
| 3495 | 3.11b4, final | Set the line number of the module’s RESUME instruction to 0, per PEP 626 |
Opcode changes in the pre-releases
The explorer reads a file with a pre-release magic number using the instruction table of
that very number. Each table was read from the opcode module of a real CPython
built from the git commit that first set the number (a commit is at
https://github.com/python/cpython/commit/<hash>). The build then compiled
60 of its own library modules, and the reader found the same code objects, instructions,
arguments, line numbers and exception tables as that build’s own dis, with no
differences, for every number in the table. The table is the one at the commit that set the
number; a commit between it and the next number can still differ. Three numbers were set
twice, with a revert between (3452, 3454 and 3455); the first commit is used.
| Magic | Change in the instruction table, against the number before | Commit |
|---|---|---|
| 3450 | The first 3.11 table. Against Python 3.10 it removes SETUP_FINALLY, SETUP_WITH, SETUP_ASYNC_WITH and POP_BLOCK (exception handling uses the exception table) and adds BEFORE_WITH, PUSH_EXC_INFO and POP_EXCEPT_AND_RERAISE | adcd2205565 |
| 3451 | Adds CALL_METHOD_KW | f24afda5917 |
| 3452 | No change to the opcode table | 9f494d49294 |
| 3453 | No change to the opcode table | 2c1e2583fdc |
| 3454 | No change to the opcode table | b2bf2bc1ece |
| 3455 | Adds MAKE_CELL; renumbers LOAD_CLOSURE 135→136, LOAD_DEREF 136→137, STORE_DEREF 137→138, DELETE_DEREF 138→139 | 631f9938b16 |
| 3456 | No change to the opcode table | ac38a9f2dfb |
| 3457 | No change to the opcode table | 355f5dd36a0 |
| 3458 | No change to the opcode table | 0d7f61ddb07 |
| 3459 | No change to the opcode table | 98eee94421d |
| 3460 | No change to the opcode table | 2f180ce2cb6 |
| 3461 | No change to the opcode table | 5e173f5db17 |
| 3462 | Adds COPY; removes COPY_DICT_WITHOUT_KEYS | 82a662e5216 |
| 3463 | No change to the opcode table | 4cdeee5978e |
| 3464 | Adds BINARY_OP; removes the 26 numeric BINARY_* and INPLACE_* instructions | 8f1b71de731 |
| 3465 | Adds COPY_FREE_VARS | 135cabd3285 |
| 3466 | Adds PREP_RERAISE_STAR, JUMP_IF_NOT_EG_MATCH | d60457a6673 |
| 3467 | Adds PRECALL_METHOD, CALL_NO_KW, CALL_KW; removes CALL_FUNCTION, CALL_FUNCTION_KW, CALL_METHOD, CALL_METHOD_KW | 9f8f45144b6 |
| 3468 | Adds SEND; removes YIELD_FROM | 0b50a4f0cde |
| 3469 | No change to the opcode table | 396b58345f8 |
| 3470 | No change to the opcode table | 65e7c1f90e9 |
| 3471 | Removes POP_EXCEPT_AND_RERAISE | a94461d7189 |
| 3472 | Removes GEN_START | 31e43cbe5f0 |
| 3473 | Adds POP_JUMP_IF_NOT_NONE, POP_JUMP_IF_NONE | 3db762db72c |
| 3474 | Adds RESUME | e028ae99ece |
| 3475 | Adds RETURN_GENERATOR, JUMP_NO_INTERRUPT | b04dfbbe4bd |
| 3476 | Adds ASYNC_GEN_WRAP | 0367a36fdc3 |
| 3477 | Adds SWAP; removes ROT_TWO, ROT_THREE, DUP_TOP, DUP_TOP_TWO, ROT_FOUR, ROT_N | 85483668647 |
| 3478 | Adds PRECALL_FUNCTION, CALL, KW_NAMES; removes CALL_NO_KW, CALL_KW | 89fd7c34520 |
| 3479 | Adds PUSH_NULL, PRECALL; removes PRECALL_FUNCTION, PRECALL_METHOD | cf345e945f4 |
| 3480 | No change to the opcode table | 59585d6b2ea |
| 3481 | Inline cache for BINARY_OP (1 entry) | 0f41aac109c |
| 3482 | Inline cache for UNPACK_SEQUENCE | 424ecab494d |
| 3483 | Inline caches for COMPARE_OP, LOAD_GLOBAL | 7820a5897e7 |
| 3484 | Inline caches for BINARY_SUBSCR, STORE_ATTR, LOAD_ATTR, LOAD_METHOD | 05a8bc1c944 |
| 3485 | GET_AWAITABLE moves from 73 to 131 and takes an argument | 586b24d3be1 |
| 3486 | Inline caches for PRECALL, CALL; CACHE becomes opcode 0 | f193631387b |
| 3487 | Inline cache for STORE_SUBSCR | 5498a61c7c2 |
| 3488 | No change to the opcode table (the low bit of LOAD_GLOBAL’s argument now says to push NULL) | 3011a097bd9 |
| 3489 | Adds JUMP_BACKWARD; removes JUMP_ABSOLUTE | a00518d9ad9 |
| 3490 | Adds CHECK_EXC_MATCH; removes JUMP_IF_NOT_EXC_MATCH | 04e07c258f4 |
| 3491 | Adds CHECK_EG_MATCH; removes JUMP_IF_NOT_EG_MATCH | 32091df41ce |
| 3492 | The POP_JUMP_IF_* instructions are split into POP_JUMP_FORWARD_IF_* and POP_JUMP_BACKWARD_IF_*, relative; adds JUMP_BACKWARD_NO_INTERRUPT (replacing JUMP_NO_INTERRUPT) | dd207a6ac52 |
| 3493 | JUMP_IF_FALSE_OR_POP and JUMP_IF_TRUE_OR_POP become relative jumps | ea2ae026078 |
| 3494 | No change to the opcode table (the location table replaces the end-line and column tables) | 37c6db60f9a |
| 3495 | No change to the opcode table; the final 3.11 table, which the released versions were checked against | 1bfe83a114d (on the 3.11 branch) |
Other changes the table does not show
- Code object. 3450 adds
co_exceptiontableafter the line table (it still hasco_nlocals,co_varnames,co_freevarsandco_cellvars). 3452 dropsco_nlocals. 3453 to 3456 writeco_localsplusnamesand then the kinds as bare bytes, one for each name, with no type byte and no length. 3457 makes the kinds a bytes object. 3459 addsco_endlinetableandco_columntableafter the line table; 3460 addsco_qualname. 3494 replaces the two extra tables with the location table. Source:marshal.cat each commit. - Columns, 3459 to 3493. The column table has two bytes for each byte of code, the start and the end column. 3459 stores the number as it is (1-based, 0 for none); from 3460 the byte is the column plus 1 and 0 means none.
- Cell variables in 3453. A cell or free variable instruction counts the
cell variables and then the free ones, taken from the names whose kind says so (a cell
that is also an argument is left out); from 3454 the number indexes
co_localsplusnames. - Lines in 3494 to 3503. The location table is decoded as in 3.11
final. At these commits CPython’s own
co_lines()looked at the wrong byte of an entry and said “no line” for some instructions that have one;co_positions()and the table agree with the explorer. - The quickened
EXTENDED_ARG. The commit that set 3495 on the 3.11 branch writes opcode 34 (EXTENDED_ARG_QUICK) for anEXTENDED_ARGin some files, because marshal did not turn the quickened form back. The explorer names it and adds its argument to the next instruction, as the interpreter does. Released 3.11 files never have it.