for root, dirs, files in os.walk(input_dir): for file in files: if file.endswith('.pyc'): pyc_path = os.path.join(root, file) rel_path = os.path.relpath(root, input_dir) out_subdir = os.path.join(output_dir, rel_path) os.makedirs(out_subdir, exist_ok=True) py_name = file.replace('.pyc', '.py') output_path = os.path.join(out_subdir, py_name) try: with open(output_path, 'w') as out_file: uncompyle6.decompile_file(pyc_path, out_file) print(f"Success: pyc_path -> output_path") except Exception as e: print(f"Failed: pyc_path - str(e)") batch_decompile("./compiled_files", "./decompiled_source") Handling Different Python Versions # Python 2.7 bytecode uncompyle6 --py-version=2.7 script.pyc Python 3.6 bytecode uncompyle6 --py-version=3.6 script.pyc Python 3.8+ bytecode uncompyle6 --py-version=3.8 script.pyc Decompiling Packaged Executables # Step 1: Extract from .exe (PyInstaller) python pyinstxtractor.py myapp.exe Step 2: Decompile extracted .pyc files uncompyle6 extracted/*.pyc Step 3: Reconstruct package structure Common Issues & Solutions Issue 1: "Unknown magic number" Error # Solution: Identify Python version first file script.pyc # Output: Python 3.8 bytecode Use correct version flag uncompyle6 --py-version=3.8 script.pyc Issue 2: Corrupted or Incomplete Bytecode # Try alternative decompiler import marshal import dis Read raw bytecode with open('script.pyc', 'rb') as f: f.read(16) # Skip header code_obj = marshal.load(f) Display disassembly for analysis dis.dis(code_obj) Issue 3: Missing Variable Names Decompiled code loses original variable names:
# Original: customer_price = calculate_discount(base_price) # Decompiled: var1 = func1(var2) : Use bytecode analysis with debug info if available. Best Practices 1. Preserve Original Files # Always backup before decompiling cp original.pyc original.pyc.backup uncompyle6 original.pyc > recovered.py 2. Verify Decompilation Quality # Test recovered code import subprocess def test_decompiled(original_pyc, recovered_py): """Compare outputs to verify correctness""" Easy Python Decompiler
:
if orig_output == recov_output: print("✓ Decompilation successful") else: print("✗ Decompilation may have errors") When decompilation produces imperfect code: for root, dirs, files in os
# Run recovered source recov_output = subprocess.check_output(['python', recovered_py]) Verify Decompilation Quality # Test recovered code import
for pattern in suspicious_patterns: if re.search(pattern, content, re.IGNORECASE): print(f"Warning: Found pattern in filepath") Easy Python Decompiler and its command-line alternatives provide effective ways to recover source code from bytecode files. While decompilation isn't perfect, it often produces workable code that can be manually corrected. Always respect intellectual property rights and use these tools ethically.