uncompyle6: Python Bytecode Decompiler

3.9.3 · active · verified Thu Apr 16

uncompyle6 is a cross-version Python bytecode decompiler. It allows you to convert Python bytecode (`.pyc` files or compiled function objects) back into human-readable Python source code. While the decompiler itself can run on modern Python versions (e.g., Python 3.6 to 3.12), it primarily supports decompiling bytecode from Python 2.x up to Python 3.8. The current version is 3.9.3, and it maintains an active release cadence, often aligning with major security conferences.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically decompile a `.pyc` file using `uncompyle6.decompile.decompile_file`. It first creates a dummy Python source file, compiles it into bytecode, and then uses `decompile_file` to print the reconstructed source code to standard output. Remember to install `uncompyle6` and its dependencies first.

import os
import py_compile
import sys
import textwrap
from pathlib import Path
from uncompyle6.decompile import decompile_file

# 1. Prepare: Create a dummy Python file and compile it to .pyc
temp_dir = Path("uncompyle6_quickstart_temp")
temp_dir.mkdir(exist_ok=True)
source_path = temp_dir / "my_module.py"
source_path.write_text(textwrap.dedent("""
    def hello_world():
        \"\"\"A simple greeting function.\"\"\"
        print("Hello from uncompyle6!")

    class MyUtil:
        def __init__(self, name):
            self.name = name
        def greet(self):
            return f"Greetings, {self.name}!"
"""))

# Compile the .py file to .pyc (ensures a standard .pyc structure)
py_compile.compile(str(source_path), cfile=str(source_path.with_suffix('.pyc')))
pyc_path = source_path.with_suffix('.pyc')

# 2. Decompile the .pyc file programmatically
try:
    print(f"--- Decompiling {pyc_path.name} ---")
    # decompile_file takes the input .pyc path and an output file-like object
    decompile_file(pyc_path, sys.stdout)
    print(f"--- Decompilation complete ---")
except Exception as e:
    print(f"An error occurred during decompilation: {e}")
finally:
    # 3. Cleanup temporary files and directory
    if temp_dir.exists():
        import shutil
        shutil.rmtree(temp_dir)

view raw JSON →