Is it possible to get some information out of the .pyc file that is generated from a .py file?
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.
-
7Thanks 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
-
50For those of you here because you accidentally deleted the wrong file, I _highly_ recommend source control! – Andrew Palmer Apr 01 '17 at 00:40
-
3^ 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
-
3`uncompyle6` is also available online at https://www.decompiler.com/ – Leo Jan 22 '21 at 12:33
-
1For VS Code users, there is no built-in local history like PyCharm, but [some plugins exist](https://stackoverflow.com/questions/46446901/how-to-see-local-history-changes-in-visual-studio-code) to do the same thing. – RichVel Jun 16 '21 at 10:01
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.
- 3,325
- 2
- 37
- 53
-
6
-
2Worked well for me (easy drag and drop). It's true that a linux distro would be nice, but its not all that hard opening a windows box. – Brian Jackson Aug 10 '17 at 16:00
-
4
-
-
“SourceForge are (sic) abusing the trust that we and our users had put into their service in the past,” according to the GIMP project. Since 2013, SourceForge has been bundling junkware along with their installers — sometimes without a developer's permission. Don't download software from SourceForge if you can help it. – MacGyver May 22 '22 at 15:20
-
1@MacGyver Sorry but these days you can't a trust link sourceforge or not, even it's a well known domain. The answer is from 2014 when https wasn't mandatory. Yes sourceforge track record has indeed gone south and I had stopped using sourceforge a long time ago. – Extreme Coders May 25 '22 at 11:00
-
1No worries. Not discrediting you. I just didn't want others to get trojens, malware, spyware, viruses, or any other internet misdemeanors. :) – MacGyver May 25 '22 at 16:57
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
- 659
- 7
- 7
-
25I 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
-
13It 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 Verkhovskiy May 23 '20 at 20:31
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.
- 10,971
- 1
- 61
- 46
Decompyle++ (pycdc) was the only one that worked for me: https://github.com/zrax/pycdc
was suggested in Decompile Python 2.7 .pyc
- 1
- 1
- 2,578
- 18
- 14
-
1And this worked for me for code that was compiled with Python 2.6! – David Mertens May 15 '13 at 20:18
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.
- 3,312
- 1
- 8
- 23
- 899
- 11
- 16
-
3While 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
I've been at this for a couple hours and finally have a solution using Decompyle++:
- visit
https://cmake.org/download/and install CMake. - visit
https://github.com/zrax/pycdcand grab a copy of this repo:pycdc-master. - add
C:\Program Files\CMake\binto 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 anotherFolderto go into the folder that haspycdc-masterin it.cmake pycdc-mastercd ../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.
-
Usually you create the `build` folder inside the repo and then inside the `build` folder you call `cmake ..` – Dmytro Ovdiienko Oct 07 '21 at 20:47
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.
- 52
- 5