Install & Compatibility
Where this runs
tested against v1.3.0 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.016s · 17.8MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.5s · import 0.016s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
parse
✓ from ast_comments import parse
Imports the specialized parse function that includes comments.
unparse
✓ from ast_comments import unparse
Imports the specialized unparse function that attempts to reconstruct comments.
Demonstrates parsing Python code to include comments in the AST, walking the tree to find `Comment` nodes, and unparsing it back to code. It also shows how to prepare the tree for Python's built-in `compile()` function by stripping comment nodes.
from ast_comments import parse, unparse, Comment, pre_compile_fixer
import ast
code = """\
def greet(name): # Function to greet a user
# This is an inline comment
message = f"Hello, {name}!" # Greeting message
return message
# Call the function
result = greet("World")
"""
# Parse the code, preserving comments
tree = parse(code)
# You can now walk the tree and find Comment nodes
comments_found = []
for node in ast.walk(tree):
if isinstance(node, Comment):
comments_found.append(node.value)
print("Comments found:")
for comment in comments_found:
print(f"- {comment}")
# Unparse the tree back to code (with comments)
# Note: formatting might differ from original, but comments are preserved
reconstructed_code = unparse(tree)
print("\nReconstructed code:")
print(reconstructed_code)
# To compile the AST, comment nodes must be removed first
fixed_tree = pre_compile_fixer(tree)
compiled_code = compile(fixed_tree, '<string>', 'exec')
# You can then exec(compiled_code) if needed
Errors
Common errors & fixes
AttributeError: 'Comment' object has no attribute 'lineno' (or 'col_offset', etc.)
Attempting to compile an AST that includes `Comment` nodes directly using Python's built-in `compile()` function or processing it with standard `ast` module utilities that expect only standard AST nodes.
fixBefore compiling or passing the AST to `ast.unparse()` (from the standard library), use `ast_comments.pre_compile_fixer(tree)` to create a new AST without `Comment` nodes. Then, compile this 'fixed' tree.
ModuleNotFoundError: cannot import name '_Unparser' from 'ast_comments.ast_analyzer' (or similar import errors on Python 3.14)
Running an older version of `ast-comments` on Python 3.14, where internal `ast` module structures and imports have changed.
fixUpgrade `ast-comments` to version 1.2.3 or later: `pip install --upgrade ast-comments`.
Comments are lost when I parse and then unparse my code.
You are likely using the standard `ast.parse()` and `ast.unparse()` functions, which do not preserve comments.
fixEnsure you are using the `parse` and `unparse` functions provided by `ast_comments`: `from ast_comments import parse, unparse`.
Upgrade
Version history
1.3.0latest on PyPI · released Feb 22, 2026
Audit
Dependencies
No dependency data recorded yet.