Introduction: The Common Misconception If you've ever lost the source code of a Python program but still have its .exe file (created with tools like PyInstaller, cx_Freeze, or py2exe), you might wonder: Can I just convert this EXE back to a .py file?
This guide explores all possible methods, their success rates, ethical considerations, and step-by-step instructions for extracting Python code from compiled executables. To understand conversion, you must first understand what a Python EXE actually contains.
def greet(name): # This comment will be lost return f"Hello, name!" print(greet("World"))
binwalk -e your_program.exe If the EXE decrypts itself only at runtime, you can dump the process memory.
python pyinstxtractor.py dist/hello.exe Inside the extracted folder, find hello.pyc .
| Original Feature | Recoverable? | |----------------|--------------| | Comments | ❌ No | | Variable names (if minified) | ❌ No (you get a , b , var1 ) | | Docstrings | ✅ Yes (if not stripped) | | Function/class names | ✅ Yes (usually) | | Original file structure (multiple .py files) | ✅ Often yes | | External library source code | ❌ Only if embedded |
The decompiled code will be – like assembly translated to Python. Part 4: Real-World Tools Comparison | Tool | Best For | Ease of Use | Success Rate | |------|----------|-------------|---------------| | pyinstxtractor | PyInstaller EXEs | Easy | High | | py2exe_extractor | Legacy py2exe | Moderate | Medium | | uncompyle6 | .pyc files | Easy | High | | decompyle3 | Python 3.8+ .pyc | Moderate | Medium-High | | strings + manual | Very old EXEs | Hard | Low | Part 5: Step-by-Step Example – Converting an EXE to PY Let’s walk through a real example using a sample EXE created with PyInstaller.
The short answer is: But the longer answer is more nuanced. While you cannot get the original source code with comments and variable names, you can often recover a large portion of the logic, reconstruct Python bytecode, and sometimes even retrieve the original .py files – depending on the tool used to create the EXE.
If you must proceed, respect intellectual property and use these techniques only on your own code or with explicit permission. # Extract PyInstaller EXE python pyinstxtractor.py target.exe Decompile single .pyc uncompyle6 file.pyc > file.py Decompile all .pyc in folder for f in *.pyc; do uncompyle6 $f > $f%.pyc.py; done Scan EXE for Python strings strings target.exe | grep -E "import |def |class " Check if EXE is PyInstaller strings target.exe | grep "PyInstaller" This guide is for educational purposes. Always ensure you have the legal right to reverse engineer any executable.
Use a decompiler like uncompyle6 or decompyle3 :
pyinstaller --onefile hello.py
Introduction: The Common Misconception If you've ever lost the source code of a Python program but still have its .exe file (created with tools like PyInstaller, cx_Freeze, or py2exe), you might wonder: Can I just convert this EXE back to a .py file?
This guide explores all possible methods, their success rates, ethical considerations, and step-by-step instructions for extracting Python code from compiled executables. To understand conversion, you must first understand what a Python EXE actually contains.
def greet(name): # This comment will be lost return f"Hello, name!" print(greet("World")) convert exe to py
binwalk -e your_program.exe If the EXE decrypts itself only at runtime, you can dump the process memory.
python pyinstxtractor.py dist/hello.exe Inside the extracted folder, find hello.pyc . Introduction: The Common Misconception If you've ever lost
| Original Feature | Recoverable? | |----------------|--------------| | Comments | ❌ No | | Variable names (if minified) | ❌ No (you get a , b , var1 ) | | Docstrings | ✅ Yes (if not stripped) | | Function/class names | ✅ Yes (usually) | | Original file structure (multiple .py files) | ✅ Often yes | | External library source code | ❌ Only if embedded |
The decompiled code will be – like assembly translated to Python. Part 4: Real-World Tools Comparison | Tool | Best For | Ease of Use | Success Rate | |------|----------|-------------|---------------| | pyinstxtractor | PyInstaller EXEs | Easy | High | | py2exe_extractor | Legacy py2exe | Moderate | Medium | | uncompyle6 | .pyc files | Easy | High | | decompyle3 | Python 3.8+ .pyc | Moderate | Medium-High | | strings + manual | Very old EXEs | Hard | Low | Part 5: Step-by-Step Example – Converting an EXE to PY Let’s walk through a real example using a sample EXE created with PyInstaller. def greet(name): # This comment will be lost
The short answer is: But the longer answer is more nuanced. While you cannot get the original source code with comments and variable names, you can often recover a large portion of the logic, reconstruct Python bytecode, and sometimes even retrieve the original .py files – depending on the tool used to create the EXE.
If you must proceed, respect intellectual property and use these techniques only on your own code or with explicit permission. # Extract PyInstaller EXE python pyinstxtractor.py target.exe Decompile single .pyc uncompyle6 file.pyc > file.py Decompile all .pyc in folder for f in *.pyc; do uncompyle6 $f > $f%.pyc.py; done Scan EXE for Python strings strings target.exe | grep -E "import |def |class " Check if EXE is PyInstaller strings target.exe | grep "PyInstaller" This guide is for educational purposes. Always ensure you have the legal right to reverse engineer any executable.
Use a decompiler like uncompyle6 or decompyle3 :
pyinstaller --onefile hello.py