astor
Read/rewrite/write Python ASTs
What it is and what it does
astor is a library for reading, rewriting, and writing Python abstract syntax trees back to source code. It specializes in round-tripping ASTs—converting them back to readable Python without requiring the modified tree to be fully compileable or include line numbers and context information. The library also provides pretty-printing of AST structures and supports non-recursive tree walking, allowing you to visit nodes and their children on your own terms rather than through forced recursion.
The package has no runtime dependencies and runs on Python 2.7 and Python 3.4 and later. It is commonly used in code generation, refactoring tools, and AST analysis where you need to inspect or transform Python code programmatically and then output human-readable results. The library includes the astor.rtrip tool for comparing two source trees functionally after transformations like PEP 8 formatting.
Use it for:
- Generate readable Python code from modified ASTs in code generation or transpilation tools.
- Compare two Python source files for functional equivalence after automated transformations.
- Debug AST structures by pretty-printing them in a more readable format than the built-in ast module.
- Build refactoring or linting tools that need to output modified code back to source.
- Implement custom AST visitors that walk the tree non-recursively and rewrite nodes based on parent context.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
astor converts Python abstract syntax trees (ASTs) back to readable source code and provides tools for AST manipulation, inspection, and non-recursive tree traversal.
Yes. astor is stable (Production/Stable status), has no dependencies, installs easily, carries no known vulnerabilities, and solves a real problem—converting ASTs back to readable code—that the Python standard library does not address well. The 2019 release date is offset by active repository maintenance and broad adoption (top 5000 packages). Install it if you need AST round-tripping or advanced tree inspection.
Install
astor on PyPI
pip
pip install astoruv
uv add astorpoetry
poetry add astorInstalling astor
Before you install
Low friction: pure Python wheel with no runtime dependencies. Maintained actively as of 2026-02-17, though the latest release dates to 2019-12-10.
License in practice
BSD-3-Clause (permissive): you may use, modify, and distribute astor freely in commercial and private projects, provided you include the license notice.
Quickstart
pip install astor==0.8.1
import ast
import astor
code = "x = 1 + 2"
tree = ast.parse(code)
print(astor.to_source(tree))
Verify before relying
- Whether the 2019-12-10 release date reflects active maintenance or if the package is in maintenance-only mode despite recent commits.
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 2,439 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 20,035,933/month — #1,047 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: astor-0.8.1-py2.py3-none-any.whl
Keywords: ast, codegen, PEP 8
Tags
More Code Generators packages
Proto Plus wraps protocol buffers to provide…
permissive · top 1,000 on PyPI
CythonCython is a source code translator that…
permissive · top 1,000 on PyPI
asttokensAnnotates Python abstract syntax trees with the…
permissive · top 1,000 on PyPI
beartypeBeartype is a runtime type checker that…
permissive · top 1,000 on PyPI
llvmlitellvmlite provides a lightweight Python binding…
permissive · top 1,000 on PyPI
gensonGenSON generates JSON Schema documents from…
permissive · top 5,000 on PyPI
astunparseConverts Python abstract syntax trees (ASTs)…
permissive · top 5,000 on PyPI
ast-decompilerConverts Python abstract syntax trees (ASTs)…
permissive · top 15,000 on PyPI
tree-sitter-juliaProvides incremental parsing of Julia source…
permissive · top 5,000 on PyPI
anytreeProvides a lightweight Python library for…
unclear · top 5,000 on PyPI
gastProvides a unified Abstract Syntax Tree…
permissive · top 5,000 on PyPI
openqasm3Provides a reference abstract syntax tree (AST)…
permissive · top 15,000 on PyPI
astprettyPretty-print Python abstract syntax trees (AST)…
permissive · top 15,000 on PyPI
promql-parserParses Prometheus query language (PromQL)…
unclear · top 15,000 on PyPI
redbaronRedBaron is a Python library for…
copyleft · top 15,000 on PyPI