pptree: Pretty print trees
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.
Common errors
-
ModuleNotFoundError: No module named 'pptree'
cause The `pptree` package has not been installed in the current Python environment.fixRun `pip install pptree` to install the library. -
NameError: name 'Node' is not defined
cause You likely used `import pptree` and then tried to directly access `Node` or `print_tree` without the `pptree.` prefix.fixEither change your import statement to `from pptree import Node, print_tree` or access the symbols using `pptree.Node` and `pptree.print_tree`. -
AttributeError: 'YourCustomNode' object has no attribute 'children'
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.fixSpecify 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')`).
Warnings
- deprecated 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.
- gotcha 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.
- gotcha 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.
- gotcha 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.
Install
-
pip install pptree
Imports
- Node
import pptree.Node
from pptree import Node
- print_tree
import pptree.print_tree
from pptree import print_tree
Quickstart
from pptree import Node, print_tree
# Create a tree using the default Node class
root = Node("Root")
child1 = Node("Child 1", root)
child2 = Node("Child 2", root)
grandchild1 = Node("Grandchild 1", child1)
print("--- Default Node Tree ---")
print_tree(root)
# Example with custom node class
class CustomNode:
def __init__(self, name, parent=None):
self.custom_name = name
self.children_list = []
if parent:
parent.children_list.append(self)
def __str__(self):
return self.custom_name
custom_root = CustomNode("Custom Root")
custom_child1 = CustomNode("Custom Child 1", custom_root)
custom_child2 = CustomNode("Custom Child 2", custom_root)
print("\n--- Custom Node Tree ---")
# For custom nodes, specify 'childattr' and 'nameattr'
print_tree(custom_root, childattr='children_list', nameattr='custom_name')