Python : How to recover python code from .pyc file

python @

Recovering the exact Python source code from a compiled .pyc file is not straightforward. The .pyc file contains the bytecode compiled from the Python source code, but it does not retain the original source code itself. However, there are a few methods you can try to decompile the .pyc file and obtain a rough approximation of the original code.

Using decompilers: There are several Python decompilers available that can help you decompile .pyc files. One popular decompiler is uncompyle6, which is capable of decompiling .pyc files from Python 3.x versions. You can install uncompyle6 using pip:

pip install uncompyle6

After installation, you can use the following command to decompile the .pyc file:

uncompyle6 file.pyc >
  1. Note that the decompiled code may not be an exact replica of the original source code, and it might be missing some comments, variable names, or formatting.
  2. Online tools: There are online services available that can attempt to decompile .pyc files. These tools generally upload your .pyc file to their server, decompile it, and provide you with the decompiled code. However, keep in mind that uploading your code to third-party services may pose security risks, so be cautious when using such tools.
  3. Disassembling the bytecode: Another approach is to disassemble the bytecode within the .pyc file using the dis module in Python. The dis module allows you to inspect the bytecode instructions, but it won’t give you the exact original source code. Nonetheless, it can provide some insights into the program’s logic and structure. Here’s an example:
import dis

def disassemble_pyc_file(pyc_file_path):
    with open(pyc_file_path, 'rb') as file:
        magic =
        timestamp =
        code_object = dis.CodeType.from_traceback(


The dis.dis() function will print the disassembled bytecode instructions to the console.

Remember that these methods can only provide an approximation of the original source code and may not fully restore the code’s functionality or readability. It’s always best to keep backups of the original source code to avoid situations where you need to recover code from compiled files.

Refer more on python here :

Author: user

Leave a Reply