Is it possible to decompile a compiled .pyc file into a .py file?
Asked Answered
E

11

254

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

Expellant answered 13/3, 2011 at 3:43 Comment(0)
B
229

Intro

Python decompilation is a hard problem, particularly for recent 3.x versions. That's why there are various tools, all with limitations and bugs.

However, Python 2.7 and earlier 3.x versions should work pretty well, and even partial decompilation is better than losing everything.

Tools to try

Uncompyle6 works, with some bugs, for Python up to 3.8, and works well for 2.7.

  • Recommended option to start with as it's aiming to unify earlier forks and focusing on automated unit testing.
  • The uncompyle6 GitHub page has more details.
  • Works best for earlier 3.x versions, not best choice for 3.7+

If that doesn't work, it's probably best to try this next - particularly for Python 3.7+:

  • decompyle3 is a fork of Uncompyle6, from same author, that should work better for 3.7 and 3.8.
  • Note: this decompyle3 package is from the rocky/decompile3 repo - different spelling but same thing

If you still have problems, check the uncompyle6 and decompyle3 READMEs which link to other tools that may work better for your code.

Limitations

You can get your code back including variable names and doc strings, but without the comments.

Some code may not successfully decompile, particularly with unusual control flow, or more recent Python 3.x versions. This is due to bugs in these decompilers where Python has changed its bytecode over time.

Supporting recent Python versions

Neither uncompyle6 or decompyle3 support Python 3.9 or higher, and support for 3.7 or higher is limited.

New optimizations in Python are making decompilation harder, and both code contributions and sponsorship are lacking for both projects.

What you can do to help:

  • Raise GitHub issues for these projects with bugs, after checking for similar issues - both run unit test suites on a range of Python versions.
  • Sponsor these projects, particularly if they helped you

Preventing loss of code in future

Frequent Git commits or backups are clearly a good idea.

Some editors/IDEs have features to help recover deleted files even if not committed to Git. See this answer for some pointers that may work in your editor or IDE, including VS Code and JetBrains IDEs such as PyCharm, GoLand and IntelliJ.

Belostok answered 11/2, 2013 at 8:27 Comment(7)
Thanks a lot. I had accidentally deleted my .py file instead of .pyc. This saved me from having to rewrite it from scratch.Dunstable
For those of you here because you accidentally deleted the wrong file, I highly recommend source control!Disaffection
^ 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.Chilblain
uncompyle6 is also available online at decompiler.comBombe
I've added a link to a new answer explaining how to undelete files in some editors and IDEs.Belostok
uncompyle6 does not support python 3.9 at the time of this comment.Belgae
@Belgae - good point, I've done an update mentioning limitations in more recent Python 3.x versions.Belostok
C
38

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.

Codicodices answered 20/2, 2014 at 18:39 Comment(9)
It does its job. Good work. BTW, did you write this tool in python?Pate
Worked 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.Opening
Last release in 2015 and looks like it's closed source?Fabron
That link looks really safe. Let me click it.Cargian
“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.Cargian
@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.Codicodices
No worries. Not discrediting you. I just didn't want others to get trojens, malware, spyware, viruses, or any other internet misdemeanors. :)Cargian
Invalid pyc/pyo file - Magic value mismatch!?Kristenkristi
@тнαтҩυιεткιᴅϟ The tool doesn't support modern Python versions. Try using an alternative.Codicodices
A
33

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.

Awn answered 6/1, 2020 at 7:42 Comment(2)
same experience here. uncompyle6 is incredible.Ipswich
At the time of writing does not support > 3.9 Python :(Slowly
H
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
Havard answered 18/4, 2011 at 14:7 Comment(3)
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.Belostok
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?Brachycephalic
@DavidChing unpyclib's first and last release was in 2009, safe to say it's a Python 2 only program.Fabron
B
16

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

was suggested in Decompile Python 2.7 .pyc

Baranowski answered 26/3, 2013 at 22:2 Comment(1)
And this worked for me for code that was compiled with Python 2.6!Frizette
R
6

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/pycdc 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.

Redbreast answered 23/7, 2021 at 1:14 Comment(1)
Usually you create the build folder inside the repo and then inside the build folder you call cmake ..Erda
R
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.

Rollin answered 17/7, 2016 at 9:23 Comment(1)
While pycdc is good, it is not perfect. If you look at 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.Pyonephritis
A
3

Install using pip install pycompyle6

pycompyle6 filename.pyc

Activist answered 9/8, 2018 at 9:11 Comment(0)
L
0

Title: Extracting Files from PyInstaller-Generated Executable

Problem:

How can I extract files from an executable created by PyInstaller?

Solution:

  1. Clone the pyinstxtractor-ng repository:

    git clone https://github.com/pyinstxtractor/pyinstxtractor-ng.git
    
  2. Extract the executable:

    cd pyinstxtractor-ng
    python pyinstxtractor-ng.py "<path_to_/dist/app>"
    
  3. Install dependencies:

    sudo yum install cmake make clang
    
  4. Clone the pycdc repository:

    git clone https://github.com/zrax/pycdc.git
    
  5. Build and compile:

    cd pycdc
    cmake CMakeLists.txt
    make
    
  6. Decompile the bytecode:

    ./pycdc "<path_to_/app_extracted/app.pyc>"
    

By following these steps, you can extract and decompile files from a PyInstaller-generated executable.

Liselisetta answered 11/3 at 9:46 Comment(0)
T
0

Please try https://pylingual.io, which supports the latest Python versions (3.6 ~ 3.12). The service implements an open service that runs an NLP-based translation model + control flow analysis.

Truong answered 19/4 at 1:53 Comment(0)
A
-1

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.

Alcibiades answered 12/8, 2021 at 15:4 Comment(2)
It does not decompile pyc made with python3.9Fairlie
At the time of writing does not support > 3.9 Python :( you can force it to run, but will not perform, sadly.Slowly

© 2022 - 2024 — McMap. All rights reserved.