Registry / serialization / pptree

pptree

JSON →
library3.1pypypi✓ verified 87d ago

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.

pip install pptree
INSTALL
IMPORT
SIG · PPTREE
P
pptree
serializationpythonv3.1
Install
2.4s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.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
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Node
from pptree import Node
import pptree.Node
The Node class is directly exposed at the top level of the pptree module when using `from pptree import ...`.
print_tree
from pptree import print_tree
import pptree.print_tree
The print_tree function is directly exposed at the top level of the pptree module when using `from pptree import ...`.

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`.

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')
Debug
Known issues
deprecatedThe `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.
fix
Evaluate newer, actively developed tree visualization libraries for long-term projects.
affects: <=3.1
gotchaSome 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.
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.
affects: All versions
gotchaThe `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.
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.
affects: All versions
gotchaWhen 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.
fix
Always explicitly define `childattr` and `nameattr` (e.g., `print_tree(root, childattr='my_children', nameattr='my_name')`) when using custom node classes.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pptree'
The `pptree` package has not been installed in the current Python environment.
fix
Run `pip install pptree` to install the library.
NameError: name 'Node' is not defined
You likely used `import pptree` and then tried to directly access `Node` or `print_tree` without the `pptree.` prefix.
fix
Either 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'
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.
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')`).
Upgrade
Version history
3.1latest on PyPI · released Apr 15, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources