Decompile Python 2.7 .pyc [closed]
Asked Answered
O

5

44

I've searched up and down, but can't find a de-compiler that will work for Python 2.7 .pyc. Does anybody know of one that will work for Python 2.7? Thanks

Overboard answered 18/11, 2011 at 21:25 Comment(0)
B
21

UPDATE (2019-04-22) - It sounds like you want to use uncompyle6 nowadays rather than the answers I had mentioned originally.

This sounds like it works: http://code.google.com/p/unpyc/

Issue 8 says it supports 2.7: http://code.google.com/p/unpyc/updates/list

UPDATE (2013-09-03) - As noted in the comments and in other answers, you should look at https://github.com/wibiti/uncompyle2 or https://github.com/gstarnberger/uncompyle instead of unpyc.

Breastsummer answered 18/11, 2011 at 21:51 Comment(8)
Even after applying the patch in issue 8, I can't get this to work with Python 2.7.Koziel
crashes with exception on Python 2.7 - use Uncompyle2Intact
I've down-voted the answer because it doesn't work with python 2.7Kassie
Yeah...even their own site says that it's broke. I wouldn't have posted it if I'd known that. Sorry!Breastsummer
uncompyle2 was great for me!Schwerin
only github.com/wibiti/uncompyle2 works for me(ubuntu17.04,python2.7.13)Mawkish
uncompyle6 is a more up to date tool, supporting Python 2.7 and 3.x - see this answer for moreIntact
uncompyle6 is also available online at decompiler.comNiue
D
85

In case anyone is still struggling with this, as I was all morning today, I have found a solution that works for me:

Uncompyle

Installation instructions:

git clone https://github.com/gstarnberger/uncompyle.git
cd uncompyle/
sudo ./setup.py install

Once the program is installed (note: it will be installed to your system-wide-accessible Python packages, so it should be in your $PATH), you can recover your Python files like so:

uncompyler.py thank_goodness_this_still_exists.pyc > recovered_file.py

The decompiler adds some noise mostly in the form of comments, however I've found it to be surprisingly clean and faithful to my original code. You will have to remove a little line of text beginning with +++ near the end of the recovered file to be able to run your code.

Decibel answered 6/4, 2012 at 18:31 Comment(11)
Saved my bacon! I'll be adding it to my virtualenvs from now on...Condor
Saved my bacon too. I was such a genius running rm *.py to cleanup my pyc files before commiting to git. But at least it left me with my .pyc files in a beautiful bit of irony.Cy
@Cy A .gitignore file containing *.pyc would be easier than having to be mindful about deleting *.pyc before each commit! Could be that you know that and are mildly obsessive about having a clean environment though, as many programmers tend to be :DDecibel
Thanks for sharing this. Had to convince my boss that its not worth it to hide .py code files.. Now he's believing me ;).Proportionate
another soul saved here!Kalin
Recommend the uncompyle2 fork of this 0 works on Python 2.7 for me: github.com/wibiti/uncompyle2Intact
I know it's a meaningless comment -- but a "thank you!!" from me as well )Effortful
Does this support Python 3?Castile
@SalmanPK no, unfortunately this was written for Python 2.7 exclusively. Someone here is working on a Python 3 decompiler, but it is still experimental: code.google.com/p/unpyc3Decibel
This saved me from an online course's ridiculous way of asserting that you've set up your environment correctly. They bundled a PYC file to make sure you couldn't read the source code to make sure numpy and matplotlib are installed! Clever, but annoying.Gestation
Online version of this decompiler is available here: javadecompilers.com/pycCislunar
L
26

Decompyle++ (pycdc) appears to work for a range of python versions: https://github.com/zrax/pycdc

For example:

git clone https://github.com/zrax/pycdc   
cd pycdc
make  
./bin/pycdc Example.pyc > Example.py
Liber answered 24/8, 2012 at 20:41 Comment(2)
True lifesaver, only one that worked for me.Craquelure
you have to run 'cmake .' before you run makePegg
B
21

UPDATE (2019-04-22) - It sounds like you want to use uncompyle6 nowadays rather than the answers I had mentioned originally.

This sounds like it works: http://code.google.com/p/unpyc/

Issue 8 says it supports 2.7: http://code.google.com/p/unpyc/updates/list

UPDATE (2013-09-03) - As noted in the comments and in other answers, you should look at https://github.com/wibiti/uncompyle2 or https://github.com/gstarnberger/uncompyle instead of unpyc.

Breastsummer answered 18/11, 2011 at 21:51 Comment(8)
Even after applying the patch in issue 8, I can't get this to work with Python 2.7.Koziel
crashes with exception on Python 2.7 - use Uncompyle2Intact
I've down-voted the answer because it doesn't work with python 2.7Kassie
Yeah...even their own site says that it's broke. I wouldn't have posted it if I'd known that. Sorry!Breastsummer
uncompyle2 was great for me!Schwerin
only github.com/wibiti/uncompyle2 works for me(ubuntu17.04,python2.7.13)Mawkish
uncompyle6 is a more up to date tool, supporting Python 2.7 and 3.x - see this answer for moreIntact
uncompyle6 is also available online at decompiler.comNiue
A
10

Here is a great tool to decompile pyc files.

It was coded by me and supports python 1.0 - 3.3

Its based on uncompyle2 and decompyle++

http://sourceforge.net/projects/easypythondecompiler/

Aret answered 28/1, 2014 at 19:4 Comment(3)
Sweet! Worth to mention, this is the one for windows users :)Prosthodontist
The source code is not available on SourceForge, so I'll ignore this tool for now.Jovita
Very handy tool! Decompiled my Python 2.7 file without any problems under wine on Linux MintPumpkinseed
K
3

Ned Batchelder has posted a short script that will unmarshal a .pyc file and disassemble any code objects within, so you'll be able to see the Python bytecode. It looks like with newer versions of Python, you'll need to comment out the lines that set modtime and print it (but don't comment the line that sets moddate).

Turning that back into Python source would be somewhat more difficult, although theoretically possible. I assume all these programs that work for older versions of Python do that.

Koziel answered 18/11, 2011 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.