Is It Possible to Decompile a Compiled. Pyc File into A. Py File?

Is it possible to get some information out of the .pyc file that is generated from a .py file?

0

9 Answers

Uncompyle6 works for Python 3.x and 2.7 - recommended option as it's most recent tool, aiming to unify earlier forks and focusing on automated unit testing. The GitHub page has more details.

  • If you use Python 3.7+, you could also try decompyle3, a fork of Uncompyle6 focusing on 3.7 and higher.
  • Do raise GitHub issues on these projects if needed - both run unit test suites on a range of Python versions.

With these tools, you get your code back including variable names and docstrings, but without the comments.

The older Uncompyle2 supports Python 2.7 only. This worked well for me some time ago to decompile the .pyc bytecode into .py, whereas unpyclib crashed with an exception.

Preventing this in future

Should move this part to a new question+answer

Using Git is strongly recommended - commit frequently on a branch.

Automated backups are good, if they are frequent enough.

Some editors/IDEs have automatic local file history features that track every save, including:

  • PyCharm: find the deleted file in the Project pane then use the local history feature to restore the file - details in this guide

Always test this works in your environment, since some tools only work in limited scenarios.

Known limitations:

  • VS Code has a local file history feature in the Timeline view since March 2022, but this may not always work to restore a deleted file:
    • If the deleted file is still open in a tab in VS Code, it can be restored, apparently even if you restart VS Code
    • Opening the deleted file again with code deleted-file.py didn't show any history, so the file could not be restored
4

You may try Easy Python Decompiler. It's based on Decompyle++ and Uncompyle2. It's supports decompiling python versions 1.0-3.3

Note: I am the author of the above tool.

7

Yes, you can get it with unpyclib that can be found on pypi.

$ pip install unpyclib

Than you can decompile your .pyc file

$ python -m unpyclib.application -Dq path/to/file.pyc
3

Yes.

I use uncompyle6 decompile (even support latest Python 3.8.0):

uncompyle6 utils.cpython-38.pyc > utils.py

and the origin python and decompiled python comparing look like this:

so you can see, ALMOST same, decompile effect is VERY GOOD.

1

Decompyle++ (pycdc) was the only one that worked for me:

was suggested in Decompile Python 2.7 .pyc

1

I've been at this for a couple hours and finally have a solution using Decompyle++:

  • visit and install CMake.
  • visit and grab a copy of this repo: pycdc-master.
  • add C:\Program Files\CMake\bin to your system environment variables under PATH.

I suggest putting your pycdc-master folder into another folder, like anotherFolder.

Now you can run these commands in the command line:

  • cd anotherFolder to go into the folder that has pycdc-master in it.
  • cmake pycdc-master
  • cd ../ to go up one directory,
  • then: cmake --build anotherFolder

pycdc.exe will then be in anotherFolder\Debug.

Do something like pycdc.exe onlyhopeofgettingmycodeback.pyc in a console and it will print out the source code. I had Python 3.9.6 source code and nothing else was working.

1

Yes, it is possible.

There is a perfect open-source Python (.PYC) decompiler, called Decompyle++

Decompyle++ aims to translate compiled Python byte-code back into valid and human-readable Python source code. While other projects have achieved this with varied success, Decompyle++ is unique in that it seeks to support byte-code from any version of Python.

1

Install using pip install pycompyle6

pycompyle6 filename.pyc

If you need to decompile a pyc but have python 3.9 installed you can force uncompyle6 to run. It's not perfect but it does work. Just edit site-packages\uncompyle6\bin\uncompile.py

def main_bin():
if not (sys.version_info[0:2] in ((2, 6), (2, 7), (3, 0),
                                  (3, 1), (3, 2), (3, 3),
                                  (3, 4), (3, 5), (3, 6),
                                  (3, 7), (3, 8), (3, 9)

Just add the version you have installed in the same format as the others and save. It will at least run.

1
Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article