AST Decompiler
raw JSON → 0.8.0 verified Fri May 01 auth: no python
Python module to decompile AST (Abstract Syntax Tree) back into Python source code. Current version 0.8.0, requires Python >=3.8. Releases are occasional, primarily maintained by the author.
pip install ast-decompiler Common errors
error ModuleNotFoundError: No module named 'ast_decompiler' ↓
cause You have not installed the package, or you installed it under a different virtual environment.
fix
Run
pip install ast-decompiler, ensure you are in the correct virtual environment or use pip3 if needed. error AttributeError: module 'ast_decompiler' has no attribute 'decompile_to_code' ↓
cause You are using an outdated function name. The function was renamed or removed.
fix
Use
decompile instead: from ast_decompiler import decompile. error AttributeError: 'str' object has no attribute 'body' ↓
cause You passed a string to decompile() instead of an AST node.
fix
Parse the string first with
ast.parse(): tree = ast.parse(code), then decompile(tree). Warnings
gotcha The import path is `ast_decompiler`, not `ast-decompiler` (with hyphen). Using `import ast-decompiler` will cause a syntax error. ↓
fix Use `from ast_decompiler import decompile` or `import ast_decompiler`.
gotcha decompile() expects an AST node, not a string. Passing a string will raise AttributeError or TypeError. ↓
fix Always parse code with ast.parse() first: tree = ast.parse(code); decompile(tree).
deprecated The old function name `decompile_to_code` may exist in very early versions or forks. It is deprecated in favor of `decompile`. ↓
fix Use `decompile` instead.
Imports
- decompile
from ast_decompiler import decompile
Quickstart
import ast
from ast_decompiler import decompile
code = """def hello(name):
return f'Hello, {name}!'
"""
tree = ast.parse(code)
reconstructed = decompile(tree)
print(reconstructed)
# Output: def hello(name):
# return f'Hello, {name}!'