For executables that are not Python-based (compiled from C/C++), full decompilation to Python is impossible. However, reverse engineering tools like Ghidra can disassemble the binary to assembly/C. You would have to manually rewrite the logic into Python—a massive undertaking.
Decompile as much as you can, use the output as pseudocode, and manually rewrite the program. This is often faster than untangling decompiler-generated spaghetti.
You can extract ~80% readable Python from most PyInstaller-packaged EXEs, but you'll never get the original formatting, comments, or imports exactly as they were.
Better than converting: Keep your original .py files backed up on GitHub. Reverse engineering is a last resort, not a workflow.
Have you successfully recovered a script from an EXE? Let me know in the comments—or share your obfuscation nightmares below. convert exe to py
Converting a Windows executable (.exe) back into Python source code (.py) is a two-step reverse-engineering process: unpacking the compiled bytecode from the executable and then decompiling that bytecode into readable text.
This guide focuses on executables created with common "freezers" like PyInstaller or py2exe, which bundle the Python interpreter and bytecode into a single file. Step 1: Unpacking the Executable
The most reliable tool for extracting the contents of a PyInstaller-generated executable is PyInstaller Extractor (pyinstxtractor).
Download the tool: Get the pyinstxtractor.py script from the official GitHub repository. For executables that are not Python-based (compiled from
Run the script: Open your terminal or command prompt in the folder containing both the script and your .exe file. Run the following command: python pyinstxtractor.py your_program.exe Use code with caution.
Locate the bytecode: A new folder named your_program.exe_extracted will be created. Inside, look for files without an extension or with a .pyc extension. The "main" script of the program is often listed in the command prompt output as a hint. Step 2: Decompiling Bytecode (.pyc) to Source (.py)
Once you have the .pyc (compiled Python bytecode) files, you need a decompiler to turn them back into readable Python code.
For Python 3.8 and older: Use uncompyle6. It is widely used and provides near-perfect reconstruction of variable names and logic. Decompile as much as you can, use the
For Python 3.9 and newer: Use PyCDC (Decompile++) or Pylingual. Tools like uncompyle6 do not support the newer bytecode structures introduced in Python 3.9+. Manual Fix: The "Magic Number"
Sometimes, extracted .pyc files are missing their "magic number" (a header that identifies the Python version used). If a decompiler fails:
Open a known-working .pyc file (from the same extracted folder) in a hex editor like HxD. Copy the first 12–16 bytes (the header).
Paste these bytes at the very beginning of your target file and save it with a .pyc extension. How to Turn your .EXE files back to precious Python code!