How to Decompile a Compiled .Pyc File into a .Py File

Decompiling PYC files for python 3.9.2

Sadly enough, it's currently impossible.
Decompile 3 has the latest pyc to py methods (decompilation),
but it hasn't updated for python 3.9 yet as that update takes a very long time to create.

And it will most likely never happen for 3.9 (the developer of decompyle3 said that he is focusing more on his main job and that he doesn't have time to create this update as the 3.9 python update really changed the workflow, so it will be very hard and time-consuming).

So for now, the only solution is to wait, but if you want to speed things up, you can always sponsor the creator of decompile 3 (https://github.com/sponsors/rocky) (as he said that if you would get enough money to work more on this project, he will)

Edit:

I have recently found out that there is an alternative

I haven't used it myself, but its meant to decompile the compiled

python file (.pyc) back to humanly readable code (.py). For any python version!

You can check it out here: https://github.com/zrax/pycdc

How to decompile pyc to py on Python 3.9?

If you really have to support 3.9, you're going to have to do it yourself.

Clone the repo locally, Change the line which requires 3.7-8, have a go, and fix it where it starts breaking (and do submit a PR when you're done). The changes between 3.8 and 3.9 are not enormous, so it likely won't be too much work. If the code you're trying to decompile is <3.9 anyway, you won't actually have to implement 3.9isms, so it may run straight off---code written in 3.8 will likely run in 3.9, as AFAIK the APIs haven't changed noticeable. I haven't looked at how compiling works, though, so I could be wrong.

How do I decompile Python 3.5 .pyc?

The ones I know about that handle Python 3.5 (and other Python versions) are:

  1. uncompyle6
  2. pycdc

[Disclamer: I develop 1]

uncompyle6 (written in Python) handles opcodes introduced in Python 3.5, while pycdc (written in C++) is still a little lacking here. But these opcodes appear only when new Python 3.5 language features are used. So the likelihood of running into this in pycdc may be small if the underlying program works on earlier versions of Python.

The situation though is a little bit different for Python 3.6 and later. Python 3.6 adds some function-call opcodes and changes the semantics of other opcodes. So in contrast to 3.5, the new opcodes appear even with code that doesn't use any of the new features used in Python 3.5, or 3.6. Python 3.7, yet again, adds method opcodes and changes the semantics of of others; and right now pycdc doesn't support that. 3.8 changes code generation a bit more than others with it's removal of SETUP_LOOP.

uncompyle6 is addressing some of these these, with the prodding of various bug reports. And with the introduction of 3.6, more of the newer 3.5 opcodes and features appear more often.

uncompyle6 is weak for 3.7 in handling control flow and even weaker for 3.8, even though it is probably the current front runner. Therefore, what I have done here is to create a new project altogether just to handle Python control flow. Just this alone is hard because of Python's rich control structures. In addition to exception handling which needs special handling to and edges in a control-flow graph, there are else blocks that can appear as part of for, while, and try structures; also, there are finally blocks.

When that project can handle things reasonably well (and right now it can't), I will put that first into forked code in project https://github.com/rocky/python-decompile3. This is hard stuff; volunteers are welcome here.

Although neither uncompyle6 nor pycdc are seriously keeping up with changes to Python, for now, uncompyle6 does a more thorough job. You can look at the issue trackers for each of the problems to get an up-to-date sense of where things stand.

Recent history with uncompyle6 and decompyle3 is that things are to the point where fixing some problems may break others. Let me explain. uncompyle6 and decompyle3 pattern match on instructions. There may be an particular pattern that fails 50% of the time. Over time I'll refine the pattern to something more complicated that fails less, say 25% of the time, but specific instances worked with the 50% pattern used previously.

More recently we have added additional checks in decompyle3 and uncompyle6 to do additional flow-control checks at grammar reduction time. However this again suggests a better rethinking is needed using control-flow dominator information. This may be done on a fork off of decompyle3.

Given this, my suggestion currently is that when there is a problem with uncompyle6 is to try different versions, use pycdc, or when practical, compare results of different decompilers.

Decompile Python 2.7 .pyc

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.

Decompile *.pyc file

You may check this tools might help you.
http://sourceforge.net/projects/unpyc/

How to decompile .pyc file to .py using Jython

As you can see in the jython documentation about modules, a jython import isn't necessarily about Java packages.

In other words: you should be able to import/require python modules as well, making it possible to rely on your pyc files!

( remember: you can import pyc files, .py files only get pulled in if they are newer than the pyc - see here )

Is it easy to fully decompile python compiled(*.pyc) files?

Depends on your definition of "fully" (in "fully decompile")...;-). You won't easily get back the original Python source -- but getting the bytecode is easy, and standard library module dis exists exactly to make bytecode easily readable (though it's still bytecode, not full Python source code;-).



Related Topics



Leave a reply



Submit