What is the PYC file format?
.pyc files are used by the Python programming language. A .pyc file is an executable that contains compiled bytecode for a program written in Python source. Bytecode is a set of instructions for the interpreter to run the program without re-parsing the original source on every launch, which speeds up import and startup time.
.pyc files are accompanied by their related .py source files. Python compiles a .py file and saves the result in a .pyc file, which can then be used to run the program directly. Since Python 3.2, these cache files are placed in a __pycache__ subdirectory alongside the source, with the interpreter version embedded in the filename (for example, script.cpython-312.pyc).
Each .pyc file begins with a 16-byte header. The first two bytes are a version-specific magic number - it changes with every CPython release - followed by 0x0D 0x0A (CR LF) at bytes 2-3. The remaining header bytes hold either a source-file timestamp and size (the default) or a source file hash for deterministic, reproducible builds, a scheme introduced by PEP 552 in Python 3.7. After the header, the file contains a marshalled code object that can be inspected with Python's built-in dis module.
.pyc bytecode is tied to a specific CPython version - a file compiled under Python 3.11 will not load under Python 3.12. The older .pyo optimized-bytecode format was removed in Python 3.5; optimized builds now produce .opt-1.pyc or .opt-2.pyc files instead. To decompile a .pyc back toward readable source, tools such as uncompyle6, decompyle3, and pycdc (Decompyle++) are commonly used, though compatibility varies by Python version. On Windows, compiled extension modules use the related .pyd format.
Security & safety
RISK: MEDIUMA .pyc is bytecode that the Python interpreter EXECUTES, so a malicious .pyc can run arbitrary code just like a script - never run a .pyc from an untrusted source. It is also a common technique for hiding malware (shipping only compiled bytecode). Conversely, distributing your own code as .pyc gives very weak protection: it can be decompiled and, at minimum, disassembled with the bundled 'dis' module, so it is NOT a real way to keep source secret. Deleting __pycache__ / .pyc files is safe - Python regenerates them automatically from the .py sources.
Format details
in a nutshellPrograms that open PYC files
Technical details
deep spec| Developer | Python Software Foundation (CPython) |
| MIME type | application/x-python-code |
| File structure | Binary - 16-byte versioned header followed by a marshalled Python code object |
| Magic number | Bytes 0-1: version-specific 2-byte value (differs per CPython release); bytes 2-3: always 0x0D 0x0A (CR LF) |
| Header layout | 32-bit magic, 32-bit bit-field flags, then either 32-bit mtime + 32-bit source size (timestamp mode) or a 32-bit source hash (PEP 552 hash mode) |
| Cache directory | Stored in __pycache__/ since Python 3.2; filename encodes the interpreter version, e.g. script.cpython-312.pyc |
| Version compatibility | Each CPython version writes a unique magic number; a .pyc from Python 3.11 will not load under 3.12 and vice versa |
| Platform portability | Platform-independent within the same CPython version - the same .pyc runs on Windows, macOS and Linux with a matching interpreter |
| Optimized variants | python -O and python -OO produce .opt-1.pyc and .opt-2.pyc (strip asserts and docstrings); the .pyo extension was retired in Python 3.5 |
| Auto-generation control | CPython writes .pyc files automatically on import; suppressed by the -B flag or PYTHONDONTWRITEBYTECODE=1 environment variable |
| Disassembly / decompilation | Python's built-in dis module disassembles bytecode; third-party tools uncompyle6, decompyle3, and pycdc (Decompyle++) attempt source reconstruction |
| Internal serialisation format | Code objects are serialised with the marshal module - a compact binary format distinct from pickle, not part of the stable public API |
| Hash-based builds | PEP 552 (Python 3.7+) introduced hash-based .pyc files: the header stores a source-file hash instead of mtime for reproducible builds |
| Released | Early CPython (1990s); current header layout (PEP 552 hash-based, deterministic) since Python 3.7, 2018 |
| Open standard | Yes · royalty-free |
| Specification | docs.python.org |
PYC conversions
Community Q&A
asked by usersNo questions yet - be the first to ask about PYC files.