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, so 3.14 starts at 3600, and the number goes up by one for each change during its development. 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.
| Release | Magic | Bytes |
|---|---|---|
| 3.14.0 and later 3.14 releases | 3627 | 2B 0E 0D 0A |
The numbers of the pre-releases are listed 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’s four bytes read as a little-endian 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
Both examples are Python 3.14 compiling the same six-byte source,
x = 1 and a line feed.
00000000 2B 0E 0D 0A 00 00 00 00 00 F1 53 65 06 00 00 00
0x6553F100 (1700000000, 14 November 2023), source size 6 bytes00000000 2B 0E 0D 0A 03 00 00 00 1D B2 B6 32 5C A2 82 A7
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.
The tag of Python 3.14 is cpython-314.
| Source | Compiled |
|---|---|
spam.py, no optimisation | __pycache__/spam.cpython-314.pyc |
spam.py with -O | __pycache__/spam.cpython-314.opt-1.pyc |
spam.py with -OO | __pycache__/spam.cpython-314.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 in CPython’s own source,
Include/internal/pycore_magic_number.h. Only 3627 is the number of a
released version. The others come from alphas, betas and release candidates, and the
release shown is the pre-release that introduced them.
| Magic | Release | What changed |
|---|---|---|
| 3600 | 3.14a1 | Add LOAD_COMMON_CONSTANT |
| 3601 | 3.14a1 | Fix the miscompilation of private names in generic classes |
| 3602 | 3.14a1 | Add LOAD_SPECIAL; remove BEFORE_WITH and BEFORE_ASYNC_WITH |
| 3603 | 3.14a1 | Remove BUILD_CONST_KEY_MAP |
| 3604 | 3.14a1 | Do not duplicate the test at the end of while statements |
| 3605 | 3.14a1 | Move ENTER_EXECUTOR to 255 |
| 3606 | 3.14a1 | Specialise CALL_KW |
| 3607 | 3.14a1 | Add the pseudo-instructions JUMP_IF_TRUE and JUMP_IF_FALSE |
| 3608 | 3.14a1 | Add support for slices |
| 3609 | 3.14a2 | Add LOAD_SMALL_INT and LOAD_CONST_IMMORTAL; remove RETURN_CONST |
| 3610 | 3.14a4 | Add the VALUE_WITH_FAKE_GLOBALS format to annotationlib |
| 3611 | 3.14a4 | Add NOT_TAKEN |
| 3612 | 3.14a4 | Add POP_ITER and INSTRUMENTED_POP_ITER |
| 3613 | 3.14a4 | Add LOAD_CONST_MORTAL |
| 3614 | 3.14a5 | Add BINARY_OP_EXTEND |
| 3615 | 3.14a5 | CALL_FUNCTION_EX always takes a kwargs argument |
| 3616 | 3.14a5 | Remove BINARY_SUBSCR and its family, making them BINARY_OPs |
| 3617 | 3.14a6 | Branch monitoring for async for loops |
| 3618 | 3.14a6 | Add an argument to END_ASYNC_FOR |
| 3619 | 3.14a6 | Renumber RESUME from 149 to 128 |
| 3620 | 3.14a6 | Optimise bytecode for all, any and tuple called on a generator expression |
| 3621 | 3.14a7 | Optimise LOAD_FAST instructions into LOAD_FAST_BORROW |
| 3622 | 3.14a7 | Store annotations in different class dict keys |
| 3623 | 3.14a7 | Add BUILD_INTERPOLATION and BUILD_TEMPLATE |
| 3624 | 3.14b1 | Do not optimise LOAD_FAST when the local is killed by DELETE_FAST |
| 3625 | 3.14b3 | Fix handling of instructions that may leave operands on the stack when optimising LOAD_FAST |
| 3626 | 3.14rc2 | Fix missing exception handlers in logical expressions |
| 3627 | 3.14rc3, final | Fix the miscompilation of some module-level annotations |