How to convert exe back to Python script

20,535

Solution 1

  1. Use pyinstxtractor.py:

         python pyinstxtractor.py yourFileName.exe
    

    This will extract .exe and create a folder named yourFileName.exe_extracted.

  2. Inside the yourFileName.exe_extracted folder, find the file without any extension.

  3. Edit it with HxD editor and from any __pycache__ file created with the same version of Python, copy the first row and insert it into your file.

  4. Save and Rename the file with .pyc extension.

  5. Decompile the compiled bytecode (.pyc) to .py using any online tool, like https://www.toolnb.com/tools-lang-en/pyc.html

Solution 2

You could try this python-exe-unpacker from In Ming Loh. Not guaranteed to work though.

Share:
20,535
S. Known
Author by

S. Known

Updated on August 16, 2022

Comments

  • S. Known
    S. Known almost 2 years

    Recently I converted a .py file to a .exe file.

    I lost the original .py file and I'm left with the exe file. I converted it with pyinstaller.

    Is there anyway to reverse this to get my original .py file back?

  • S. Known
    S. Known almost 6 years
    How does it work? I came as far as pyinstxtractor.py help.exe and the result was You can now use a python decompiler on the pyc files within the extracted directory so what do I have to do now?
  • hjpotter92
    hjpotter92 almost 6 years
    Use a python decompiler on thepycfile.
  • S. Known
    S. Known almost 6 years
    @hjpotter92 there are 204 pyc files
  • S. Known
    S. Known almost 6 years
    Yes Thanks for the info but still, there are 204 .pyc files. Do I just go over them 1 by 1?? @hjpotter92
  • hjpotter92
    hjpotter92 almost 6 years
    @S.Known Did you bother clicking on any of provided alternatives for decompiling? Did you read its documentation? Did you search for "decompiling a directory of pyc files"? Have you heard of using * as wildcard?
  • S. Known
    S. Known almost 6 years
    @hjpotter92 I have searched for alternatives for decompiling but I think decompyle6 is the way to go. But now I dont need to decompyle all 204 files because the most of them are actualy the libraries I used with their components. I just need to decompyle the file I made, but I dont know wich one it is from the 204.
  • rinkert
    rinkert almost 6 years
    @S.Known, we don't know either, but if you can exclude the libraries like you said, there should be a limited number left, which you can decompile in one batch. Then search for your file...
  • S. Known
    S. Known almost 6 years
    @rickert I have made a little code in powershell that uncompyled everything and put everything into a folder I made. After that I searched trough all the .py files with notepad++ a couple times for a line of my code. But no luck. Couldnt find it. Is there a other way?
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • Flair
    Flair over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review