{"id":8439,"library":"pptree","title":"pptree: Pretty print trees","description":"pptree is a Python library (current version 3.1) designed to pretty-print tree-like data structures in a console-friendly, hierarchical format. It provides a default `Node` implementation and a `print_tree` function that can work with both its own `Node` objects and custom object structures. The library has an infrequent release cadence, with the last update in April 2020.","status":"maintenance","version":"3.1","language":"en","source_language":"en","source_url":"https://github.com/clemtoy/pptree","tags":["tree","pretty-print","visualization","data-structure","console"],"install":[{"cmd":"pip install pptree","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The Node class is directly exposed at the top level of the pptree module when using `from pptree import ...`.","wrong":"import pptree.Node","symbol":"Node","correct":"from pptree import Node"},{"note":"The print_tree function is directly exposed at the top level of the pptree module when using `from pptree import ...`.","wrong":"import pptree.print_tree","symbol":"print_tree","correct":"from pptree import print_tree"}],"quickstart":{"code":"from pptree import Node, print_tree\n\n# Create a tree using the default Node class\nroot = Node(\"Root\")\nchild1 = Node(\"Child 1\", root)\nchild2 = Node(\"Child 2\", root)\ngrandchild1 = Node(\"Grandchild 1\", child1)\n\nprint(\"--- Default Node Tree ---\")\nprint_tree(root)\n\n# Example with custom node class\nclass CustomNode:\n    def __init__(self, name, parent=None):\n        self.custom_name = name\n        self.children_list = []\n        if parent:\n            parent.children_list.append(self)\n\n    def __str__(self):\n        return self.custom_name\n\ncustom_root = CustomNode(\"Custom Root\")\ncustom_child1 = CustomNode(\"Custom Child 1\", custom_root)\ncustom_child2 = CustomNode(\"Custom Child 2\", custom_root)\n\nprint(\"\\n--- Custom Node Tree ---\")\n# For custom nodes, specify 'childattr' and 'nameattr'\nprint_tree(custom_root, childattr='children_list', nameattr='custom_name')","lang":"python","description":"This quickstart demonstrates how to create and pretty-print a tree using `pptree`'s default `Node` class and how to adapt `print_tree` for custom node implementations by specifying `childattr` and `nameattr`."},"warnings":[{"fix":"Evaluate newer, actively developed tree visualization libraries for long-term projects.","message":"The `pptree` library appears to be in maintenance mode, with its last release in April 2020. Consider actively maintained alternatives like `treelib` or `PrettyPrintTree` for more features and ongoing support.","severity":"deprecated","affected_versions":"<=3.1"},{"fix":"Test `pptree` output in your target environment. For critical applications, consider alternative libraries or rendering methods, or explore `print-tree2` which claims better cross-platform Unicode support.","message":"Some users have reported potential display issues with Unicode characters or inconsistent spacing in certain terminal environments, particularly on Windows, when using `pptree` or similar console-based tree printers.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Do not rely on `pptree` to preserve the exact order of children in the printed output if order is critical. Verify if an alternative library respects child order.","message":"The `print_tree` function may reorder the children nodes from their original insertion order during display. If the order of children is semantically significant for your tree structure, this behavior can be misleading or incorrect.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always explicitly define `childattr` and `nameattr` (e.g., `print_tree(root, childattr='my_children', nameattr='my_name')`) when using custom node classes.","message":"When using custom node objects with `print_tree`, the `childattr` and `nameattr` parameters must accurately reflect the attribute names in your custom class for its children list and displayable name, respectively. Incorrect values will lead to `AttributeError` or unexpected tree structures.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install pptree` to install the library.","cause":"The `pptree` package has not been installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'pptree'"},{"fix":"Either change your import statement to `from pptree import Node, print_tree` or access the symbols using `pptree.Node` and `pptree.print_tree`.","cause":"You likely used `import pptree` and then tried to directly access `Node` or `print_tree` without the `pptree.` prefix.","error":"NameError: name 'Node' is not defined"},{"fix":"Specify the correct attribute name for children in your custom node using the `childattr` parameter (e.g., `print_tree(my_root, childattr='my_custom_children_attribute')`).","cause":"When using `print_tree` with a custom node class, the default `childattr='children'` was used, but your custom node class does not have an attribute named `children` to store its child nodes.","error":"AttributeError: 'YourCustomNode' object has no attribute 'children'"}]}