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.13 starts at 3550, and the number goes up by one for each change within the 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.13 release writes 3571, the bytes F3 0D 0D 0A. The
numbers 3550 to 3570 come from the alphas; 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
Both files were written by Python 3.13 with py_compile, from a six-byte
source file, x = 1 and a line feed.
00000000 F3 0D 0D 0A 00 00 00 00 00 F1 53 65 06 00 00 00
0x6553F100 (1700000000, 14 November 2023), source size 6 bytes00000000 F3 0D 0D 0A 03 00 00 00 E7 86 E2 89 36 51 12 0E
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, no optimisation | __pycache__/spam.cpython-313.pyc |
spam.py, with -O | __pycache__/spam.cpython-313.opt-1.pyc |
spam.py, with -OO | __pycache__/spam.cpython-313.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.13’s own source, in the comments of
Lib/importlib/_bootstrap_external.py. The number marked final is the one
every released 3.13 version writes. The others come from alphas, and the release shown
is the pre-release that introduced them.
| Magic | Release | What changed |
|---|---|---|
| 3550 | 3.13a1 | Plugin optimizer support |
| 3551 | 3.13a1 | Compact superinstructions |
| 3552 | 3.13a1 | Remove LOAD_FAST__LOAD_CONST and LOAD_CONST__LOAD_FAST |
| 3553 | 3.13a1 | Add SET_FUNCTION_ATTRIBUTE |
| 3554 | 3.13a1 | More efficient bytecodes for f-strings |
| 3555 | 3.13a1 | Generate specialised opcode metadata from bytecodes.c |
| 3556 | 3.13a1 | Convert LOAD_CLOSURE to a pseudo-instruction |
| 3557 | 3.13a1 | Make the conversion to boolean in jumps explicit |
| 3558 | 3.13a1 | Reorder the stack items for CALL |
| 3559 | 3.13a1 | Generate opcode numbers from bytecodes.c |
| 3560 | 3.13a1 | Add RESUME_CHECK |
| 3561 | 3.13a1 | Add a cache entry to branch instructions |
| 3562 | 3.13a1 | Assign numbers for internal instructions in a separate range |
| 3563 | 3.13a1 | Add CALL_KW, remove KW_NAMES |
| 3564 | 3.13a1 | Remove YIELD_VALUE’s argument, change RESUME’s argument values |
| 3565 | 3.13a1 | YIELD_VALUE’s argument says whether it is in a yield-from |
| 3566 | 3.13a1 | Emit JUMP_NO_INTERRUPT instead of JUMP for non-loop cases without a line number |
| 3567 | 3.13a1 | Reimplement line number propagation by the compiler |
| 3568 | 3.13a1 | Change the meaning of END_FOR |
| 3569 | 3.13a5 | Specialise CONTAINS_OP |
| 3570 | 3.13a6 | Add the __firstlineno__ class attribute |
| 3571 | 3.13b1, final | Fix the miscompilation of private names in generic classes |