Convert py to pyc

Convert PY to PYC

Create CPython bytecode from PY source with the correct local interpreter and version.

How to convert py to pyc file

A deployment environment may require CPython bytecode instead of a source file, or a developer may want the import cache generated for a project. Creating a .pyc file is a local compilation step performed by the Python interpreter that will run it, not a general document conversion.

What the PY format is

.py is a plain-text Python source file containing statements, functions, classes, imports, comments, and other program code. Developers create and edit PY files in Visual Studio Code, PyCharm, IDLE, or any text editor. CPython parses the source and compiles it to bytecode in memory before execution; imported modules may also have that bytecode written to a PYC cache file.

What the PYC format is

.pyc is a binary bytecode file produced by CPython. Its header contains the interpreter’s bytecode identifier and cache-validity information; the remainder contains serialized Python bytecode. CPython normally stores generated files in __pycache__, using names such as module.cpython-312.pyc.

A cache can avoid recompiling an unchanged imported module, but it is not an executable replacement for the whole application and does not hide source code securely. Bytecode can be inspected or decompiled, and a PYC file remains tied to a particular Python implementation and version.

How to create a PYC file

Install the official CPython version required by the target system, then open a terminal in the directory containing the source file. The interpreter’s python command may be named python3 on some systems.

  1. Compile one source file into the normal cache location:
    python -m py_compile script.py
  2. Compile all Python files in a directory, including subdirectories:
    python -m compileall project_folder
  3. Put the generated PYC beside the source file instead of in __pycache__:
    python -m compileall -b script.py
  4. Specify an exact output filename with the standard library:
    python -c "import py_compile; py_compile.compile('script.py', cfile='output/script.pyc')"

The parent directory for the cfile path must already exist. Use the same commands with python3 if that is the installed interpreter name. Running a script directly normally compiles it in memory; importing a module is what ordinarily creates its cache file. Python-aware IDEs use their configured interpreter and may generate PYC files while running or importing project modules.

Use local CPython rather than an online converter. A web service would receive the source code and might compile it with the wrong Python version; there is no general-purpose online conversion service that is preferable for ordinary PY-to-PYC work. An IDE or desktop terminal provides the same compilation through the selected local interpreter.

Compatibility and limitations

A PYC created by CPython 3.12 generally cannot be used by CPython 3.11, Python 2, or a different Python implementation. Compile with the same major and minor version, and preferably the same implementation, used in the deployment environment. CPython can ignore and regenerate a cache when its timestamp-and-size data or hash-based validity data no longer matches the source.

Compilation detects syntax errors but does not test program behavior. Runtime dependencies, import paths, operating-system features, configuration, and runtime errors remain unresolved. Imported packages must still be installed unless the application is packaged separately. For distribution, use a wheel, virtual environment, executable bundle, or container when appropriate; isolated PYC files are mainly interpreter caches or implementation-specific deployment artifacts.

PY vs PYC: format comparison

How the PY and PYC formats compare on the properties that matter most for this conversion.

Comparison of the PY and PYC file formats
Property .PY Python source file .PYC Python compiled bytecode
Plain-text readable Yes No
Text formatting Basic None
Images No No
Further editing Easy Not directly editable
Opens in a web browser No No
Typical file size Small Very small
Open standard Yes Yes
Best used for Editing and post-production Embedding in applications
Introduced 1991 1991
Developer Python Software Foundation Python Software Foundation
MIME type text/x-python —

Additional formats for
py file conversion

Reverse conversion

Convert to pyc from
other formats

Share on social media: