Install & Compatibility
Where this runs
tested against v0.8.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.032s · 18MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.026s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
to_source
✓ from astor import to_source
parse_file
✓ from astor import parse_file
✗ from astor import parsefile
`parsefile()` was deprecated in version 0.6; use `parse_file()` instead.
dump_tree
✓ from astor import dump_tree
✗ from astor import dump
`dump()` was deprecated in version 0.6; use `dump_tree()` instead.
SourceGenerator
✓ from astor import SourceGenerator
✗ from astor.codegen import SourceGenerator
`SourceGenerator` can be imported directly from `astor` since version 0.8.0. The `astor.codegen` submodule is deprecated and raises a `DeprecationWarning`.
This quickstart demonstrates how to parse a Python source code string into an Abstract Syntax Tree (AST) using Python's built-in `ast` module, and then convert it back into formatted Python source code using `astor.to_source`.
import ast
import astor
# Example Python code as a string
source_code = """
def greet(name):
message = f"Hello, {name}!"
return message
"""
# Parse the source code into an AST
tree = ast.parse(source_code)
# Convert the AST back to source code
generated_source = astor.to_source(tree)
print("Original code:\n" + source_code)
print("\nGenerated code from AST:\n" + generated_source)
Debug
Known issues
breakingThe `ast` module in Python frequently changes between minor versions (e.g., 3.8, 3.9, 3.10+). While `astor` 0.8.1 has some compatibility fixes for Python 3.8 features (like `ast.Constant` and assignment expressions), it is unlikely to fully support AST changes introduced in Python 3.9 and newer (e.g., `match/case` in 3.10, `type statement` in 3.12). Users on Python 3.9+ might encounter issues or incorrect code generation.fixConsider using `ast.unparse` (available since Python 3.9) for basic AST-to-source conversion, or `ast-compat` for cross-version AST construction. For complex manipulations with `astor`, you may need to pin to older Python versions or investigate community forks/alternatives.
affects: 0.8.1 on Python >=3.9
deprecatedSeveral functions and submodules have been deprecated since `astor` 0.6 and 0.8. The `astor.codegen` submodule, `astor.parsefile()`, and `astor.dump()` are officially deprecated.fixUse `from astor import SourceGenerator` instead of `from astor.codegen import SourceGenerator`. Use `astor.parse_file()` instead of `astor.parsefile()`. Use `astor.dump_tree()` instead of `astor.dump()`.
affects: >=0.6
gotchaWhen using `astor.code_to_ast()` with decorated functions in Python 3.8.1, a `KeyError` could occur due to mismatches in line numbers in the internal cache. This issue was resolved in a commit that shipped with version 0.8.1.fixEnsure you are using `astor` version 0.8.1 or newer to avoid this specific `KeyError` when dealing with decorated functions on Python 3.8.1 and above.
affects: <0.8.1 (on Python 3.8.1+)
deprecatedPython 3.12 (and later 3.x versions) deprecates old AST node types like `ast.Num`, `ast.Str`, `ast.Bytes`, `ast.NameConstant`, and `ast.Ellipsis` in favor of `ast.Constant`. Using `astor` on these newer Python versions may produce `DeprecationWarning`s or fail to handle ASTs correctly if it relies on these removed nodes.fixBe aware of these deprecations. While `astor` might attempt to handle them, for full compatibility with future Python versions, custom AST transformations may need to be updated to use `ast.Constant`. There is an open issue regarding this for `astor` (Issue #217).
affects: 0.8.1 on Python >=3.12
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'astor'
The 'astor' module is not installed in the current Python environment.
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\me\\AppData\\Local\\Temp\\_MEI138162\\astor\\VERSION'
When using PyInstaller, the 'VERSION' file required by 'astor' is not included in the build.
fixManually include the 'VERSION' file from the 'astor' package directory into the PyInstaller build.
DeprecationWarning: ast.Num is deprecated and will be removed in Python 3.14; use ast.Constant instead
The 'ast.Num' node type is deprecated in Python 3.12 and will be removed in future versions.
fixUpdate code to use 'ast.Constant' instead of 'ast.Num'.
NameError: name 'astor' is not defined
The 'astor' module is used without being imported.
AttributeError: No defined handler for node of type Match
This error occurs when `astor.to_source()` encounters an AST node type (like `Match` from Python 3.10+ `match/case` statements) that the installed version of astor does not support or know how to convert back into source code.
fixUpgrade astor to a version that supports the specific AST node, or if no newer version is available (astor is slowly maintained), consider using an alternative library like `libcst` for AST manipulation with newer Python syntax. If upgrading is not an option, you might need to manually handle or transform unsupported nodes before calling `to_source`.
Upgrade
Version history
0.8.1latest on PyPI · released Dec 10, 2019
Audit
Dependencies
No dependency data recorded yet.