{"id":8748,"library":"uncompyle6","title":"uncompyle6: Python Bytecode Decompiler","description":"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.","status":"active","version":"3.9.3","language":"en","source_language":"en","source_url":"https://github.com/rocky/python-uncompyle6/","tags":["decompilation","bytecode","reverse-engineering","security","python2","python3"],"install":[{"cmd":"pip install uncompyle6","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for bytecode disassembly and parsing. Frequent API changes in xdis can necessitate uncompyle6 updates.","package":"xdis","optional":false}],"imports":[{"symbol":"decompile_file","correct":"from uncompyle6.decompile import decompile_file"},{"note":"This is a lower-level function requiring raw bytecode and version info, typically used internally or for more advanced cases than `decompile_file`.","symbol":"decompile","correct":"import uncompyle6.main\nuncompyle6.main.decompile(...)"}],"quickstart":{"code":"import os\nimport py_compile\nimport sys\nimport textwrap\nfrom pathlib import Path\nfrom uncompyle6.decompile import decompile_file\n\n# 1. Prepare: Create a dummy Python file and compile it to .pyc\ntemp_dir = Path(\"uncompyle6_quickstart_temp\")\ntemp_dir.mkdir(exist_ok=True)\nsource_path = temp_dir / \"my_module.py\"\nsource_path.write_text(textwrap.dedent(\"\"\"\n    def hello_world():\n        \\\"\\\"\\\"A simple greeting function.\\\"\\\"\\\"\n        print(\"Hello from uncompyle6!\")\n\n    class MyUtil:\n        def __init__(self, name):\n            self.name = name\n        def greet(self):\n            return f\"Greetings, {self.name}!\"\n\"\"\"))\n\n# Compile the .py file to .pyc (ensures a standard .pyc structure)\npy_compile.compile(str(source_path), cfile=str(source_path.with_suffix('.pyc')))\npyc_path = source_path.with_suffix('.pyc')\n\n# 2. Decompile the .pyc file programmatically\ntry:\n    print(f\"--- Decompiling {pyc_path.name} ---\")\n    # decompile_file takes the input .pyc path and an output file-like object\n    decompile_file(pyc_path, sys.stdout)\n    print(f\"--- Decompilation complete ---\")\nexcept Exception as e:\n    print(f\"An error occurred during decompilation: {e}\")\nfinally:\n    # 3. Cleanup temporary files and directory\n    if temp_dir.exists():\n        import shutil\n        shutil.rmtree(temp_dir)","lang":"python","description":"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."},"warnings":[{"fix":"Verify the Python version that compiled the `.pyc` file. For bytecode from Python 3.9+, you might need to use other tools or wait for future uncompyle6 updates supporting those versions.","message":"uncompyle6 can run on modern Python versions (e.g., 3.6-3.12), but it primarily supports decompiling bytecode up to Python 3.8. Attempting to decompile `.pyc` files from newer Python versions will likely result in errors or incorrect output.","severity":"gotcha","affected_versions":"All versions of uncompyle6 when used with Python 3.9+ bytecode."},{"fix":"Always install `uncompyle6` using `pip install uncompyle6` to ensure compatible `xdis` versions are pulled in. If issues arise, try `pip install uncompyle6 --upgrade` to get the latest compatible versions of both libraries.","message":"The `xdis` library, a core dependency, frequently undergoes API changes. These changes can break `uncompyle6` if its bundled or required `xdis` version becomes incompatible with a newly installed or updated `xdis`.","severity":"breaking","affected_versions":"Versions 3.7.0, 3.9.1, 3.9.2, 3.9.3 (and potentially others) due to explicit mentions in release notes."},{"fix":"If you need to recover docstrings or assert statements, ensure the original `.pyc` file was compiled without Python's optimization flags (`-O` or `-OO`).","message":"When Python code is compiled with optimization flags (`python -O` or `python -OO`), certain elements like docstrings and `assert` statements are removed from the bytecode. Decompiling such `.pyc` files will result in missing these elements.","severity":"gotcha","affected_versions":"All versions, as this is a characteristic of Python's bytecode optimization."}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `xdis` is installed: `pip install xdis` or reinstall uncompyle6 to ensure dependencies are resolved: `pip install uncompyle6 --upgrade`.","cause":"The essential `xdis` dependency was not installed or is not accessible in the current environment.","error":"ModuleNotFoundError: No module named 'xdis'"},{"fix":"Verify the `.pyc` file's integrity and its original Python version. `uncompyle6` supports up to Python 3.8 bytecode. Using a `.pyc` from Python 3.9+ will likely cause this error.","cause":"The `.pyc` file is either corrupted, from a significantly different Python version than `uncompyle6` expects, or is not a valid Python bytecode file.","error":"uncompyle6.scanner.TokenError: Bad magic number for file ..."},{"fix":"This indicates that `uncompyle6` does not support decompiling bytecode from that specific Python version. `uncompyle6` primarily supports up to Python 3.8. For newer versions, consider alternative decompilers or check for major uncompyle6 updates.","cause":"The `.pyc` file contains opcodes introduced in a newer Python version (e.g., 3.9, 3.10, etc.) that `uncompyle6` does not yet have grammar rules for.","error":"uncompyle6.scanner.TokenError: Unknown opcode: NNN (where NNN is a number)"}]}