Why convert PY to PYC?
Pre-compiling to .pyc lets Python skip the parse step on the next run, giving a minor startup speed improvement. It also provides mild obfuscation since the bytecode is not human-readable source, though it can be decompiled with tools like uncompyle6.
How to convert PY to PYC
Python (py_compile) FREE
Run python -m py_compile yourscript.py in a terminal; the .pyc file appears in the __pycache__ subfolder alongside your script, named with the Python version tag.
Python (compileall) FREE
Run python -m compileall /path/to/project to compile every .py file in a directory tree at once; all .pyc files appear in their respective __pycache__ subfolders.
About these formats
A .py file is a Python source code file - plain text containing a program or script written in the Python language. To read the code, open it in any text editor. To run it, install Python…
Open .PY details →A .pyc file is compiled Python bytecode that CPython caches automatically to speed up startup. You do not normally need to open or touch it. To run one, use the matching Python version. To…
Open .PYC details →Quality & what to watch
- A
.pycfile is tied to the exact Python version that compiled it - a file compiled with Python 3.12 will not load in Python 3.11 or earlier, because each version embeds a unique magic number in the header. .pycis not real source protection - tools such asuncompyle6can decompile bytecode back to readable Python source; do not rely on it to protect intellectual property.- Python 3 always writes
.pycfiles into a__pycache__subfolder, never next to the source file; the old.pyooptimized bytecode extension was removed in Python 3.5.
Frequently asked questions
Do I need to manually compile to PYC, or does Python do it automatically?
.pyc automatically the first time it runs. The manual py_compile command is useful for pre-compiling without running or for catching syntax errors early.Can I distribute only the PYC files without the original PY source?
.pyc files are version-specific and can be decompiled. Standard practice is to distribute the .py source instead.Where does Python put the PYC file?
__pycache__ subfolder next to the .py source, with a name like yourscript.cpython-312.pyc reflecting the Python version that compiled it.