{"id":5116,"library":"astpretty","title":"astpretty","description":"astpretty is a Python library designed to pretty-print the Abstract Syntax Tree (AST) generated by Python's standard library `ast.parse` function. It offers a more readable, indented, and detailed representation of AST nodes compared to the built-in `ast.dump`, making it useful for debugging and understanding code structure. The current version is 3.0.0, released in May 2022, requiring Python 3.8+. It's a stable, focused utility with infrequent but significant updates.","status":"active","version":"3.0.0","language":"en","source_language":"en","source_url":"https://github.com/asottile/astpretty","tags":["AST","pretty-print","developer-tool","debugging"],"install":[{"cmd":"pip install astpretty","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"While 'from astpretty import pprint' works, the recommended usage in documentation is 'astpretty.pprint' to avoid name collisions with the standard library's 'pprint' module.","wrong":"from astpretty import pprint","symbol":"pprint","correct":"import astpretty\nastpretty.pprint(node)"},{"symbol":"pformat","correct":"import astpretty\nresult = astpretty.pformat(node)"}],"quickstart":{"code":"import ast\nimport astpretty\n\ncode_snippet = \"\"\"\ndef greet(name):\n    print(f'Hello, {name}!')\n\"\"\"\n\n# Parse the code into an AST node\nparsed_ast = ast.parse(code_snippet)\n\n# Pretty print the entire AST\nastpretty.pprint(parsed_ast)\n\n# Or pretty print a specific node, e.g., the function definition\n# astpretty.pprint(parsed_ast.body[0], show_offsets=False, indent='  ') # Example with options","lang":"python","description":"This example demonstrates parsing a simple Python code snippet into an AST using the standard `ast` module and then using `astpretty.pprint` to display its structured, human-readable representation. You can also use `astpretty.pformat` to get the pretty-printed string instead of printing directly to stdout."},"warnings":[{"fix":"Upgrade your Python environment to 3.8 or newer. For Python 2, use `astpretty < 2.0.0` (not recommended for new development).","message":"Python 2 support was dropped with `astpretty` version 2.0.0. Version 3.0.0 further increased the minimum Python requirement to 3.8. Older Python 2 or early Python 3 projects will need to use an older `astpretty` version or upgrade their Python interpreter.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"For less verbosity, use `astpretty.pprint(node, show_offsets=False)`. For Python 3.9+, consider `ast.dump(node, indent=...)` if a simpler, indented string is sufficient. `astpretty` generally offers more detailed AST information.","message":"`astpretty.pprint` provides a highly detailed, multi-line, indented output, including `lineno` and `col_offset` by default. This is significantly more verbose than the standard library's `ast.dump`, which outputs a single-line representation. Users accustomed to `ast.dump` might find `astpretty`'s default output overwhelming if they only need a compact string.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `import ast` and `ast.parse(your_code_string)` to obtain an AST node, then pass that node to `astpretty` functions.","message":"`astpretty` operates on an existing AST node object, not directly on source code strings. You must first parse your code into an AST object using `ast.parse` from the Python standard library before passing it to `astpretty.pprint` or `astpretty.pformat`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}