196

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

Braiam
  • 4,345
  • 11
  • 47
  • 69
Howard
  • 1,961
  • 2
  • 12
  • 3

7 Answers7

185

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 decompile3, 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.

RichVel
  • 3,992
  • 4
  • 24
  • 38
  • 7
    Thanks a lot. I had accidentally deleted my .py file instead of .pyc. This saved me from having to rewrite it from scratch. – avmohan Jan 09 '14 at 09:39
  • 29
    For those of you here because you accidentally deleted the wrong file, I _highly_ recommend source control! – Andrew Palmer Apr 01 '17 at 00:40
  • 2
    ^ And if you're using PyCharm, you can right-click your file/folder in the Project pane and goto Local History > Show History to revert changes. Life saver. – mattshu Oct 13 '20 at 19:22
  • 2
    `uncompyle6` is also available online at https://www.decompiler.com/ – Leo Jan 22 '21 at 12:33
30

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.

Extreme Coders
  • 3,195
  • 2
  • 33
  • 49
29

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
Pokutnik
  • 651
  • 7
  • 7
  • 23
    I tried this and it crashed with an exception, on quite a small file with no complex code (Django settings.py) - uncompyle2 worked fine instead. -1 for that reason. – RichVel Feb 11 '13 at 08:25
  • 11
    It crashed in Python 3.6 in lib\site-packages\unpyclib\applcation.py with `print __copyright` -- why is it using the Python 2.7 version of `print` without parenthesis? – David Ching Jun 07 '18 at 23:46
  • 3
    @DavidChing `unpyclib`'s first and last release [was in 2009](https://pypi.org/project/unpyclib/#history), safe to say it's a Python 2 only program. – Boris May 23 '20 at 20:31
13

Decompyle++ (pycdc) was the only one that worked for me: https://github.com/zrax/pycdc

was suggested in Decompile Python 2.7 .pyc

Community
  • 1
  • 1
Kenneth Hoste
  • 2,428
  • 17
  • 14
12

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:

pyc uncompile utils

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

crifan
  • 8,424
  • 1
  • 45
  • 35
4

Yes, it is possible.

There is a perfect open-source Python (.PYC) decompiler, called Decompyle++ https://github.com/zrax/pycdc/

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.

G_M
  • 3,232
  • 1
  • 7
  • 22
Andrew Rukin
  • 886
  • 11
  • 15
  • 3
    While pycdc is good, it is not perfect. If you look at https://github.com/zrax/pycdc/issues there are over 50 individual types of problems it has in decompilation. This is however spread over the 16 or so releases of python, and both the language and code has changed drastically. It may be that for the things you have tried you haven't been able to find a problem. However, in my opinion, to classify something as "perfect", one would have to take say the entire Python library for each version, decompile it, and have it pass its own tests properly. No decompiler can do that yet. – rocky Jan 08 '18 at 20:51
1

Install using pip install pycompyle6

pycompyle6 filename.pyc