Registry / data / treelib

treelib

JSON →
library1.8.0pypypi✓ verified 24d ago

TreeLib provides a Python implementation for creating, manipulating, and visualizing tree data structures. It allows users to build hierarchical data, traverse nodes, and render trees to various outputs like console text or Graphviz. It is actively maintained with a moderate release cadence, with the current version being 1.8.0.

pip install treelib
INSTALL
IMPORT
SIG · TREELIB
T
treelib
datapythonv1.8.0
Install
1.6s avg
Import
39ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.8.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
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.042s · 18.1MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.035s · 19MB
16MB installed
● package 16MB
Code
Verified usage

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

Tree
from treelib import Tree

This example demonstrates creating a new tree, adding several nodes with unique identifiers and parent-child relationships, and then printing the tree to the console.

from treelib import Tree tree = Tree() tree.create_node("Harry", "harry") # Root node tree.create_node("Jane", "jane", parent="harry") tree.create_node("Bill", "bill", parent="harry") tree.create_node("Diane", "diane", parent="jane") tree.create_node("George", "george", parent="jane") tree.create_node("Mark", "mark", parent="bill") tree.show()
Debug
Known issues
breakingtreelib v1.8.0 and later no longer support Python 3.6 due to its End-of-Life. Attempting to install or run treelib 1.8.0+ on Python 3.6 will result in errors.
fix
Upgrade your Python interpreter to 3.7 or higher. If you must use Python 3.6, you will need to pin treelib to a version prior to 1.8.0 (e.g., `treelib<1.8`).
affects: >=1.8.0
breakingThe `treelib.plugin` module, including `BaseConnector` and `NodeMixin`, was removed in v1.7.0. Code relying on these deprecated components will break.
fix
Refactor code to avoid the `plugin` module. For custom node behavior or mixins, use standard Python inheritance or create your own mixin classes directly without depending on `treelib.plugin.NodeMixin`.
affects: >=1.7.0
gotchaMethods like `Tree.to_graphviz()` require the `graphviz` Python package (and the Graphviz system library) to be installed separately. Without these, such methods will raise an `ImportError` or similar error.
fix
Install the `graphviz` Python package with `pip install treelib[graphviz]` or `pip install graphviz`. Additionally, ensure the Graphviz system utility is installed on your operating system and discoverable in your PATH.
affects: all
gotchaEach node in a `treelib.Tree` must have a unique `identifier`. Reusing identifiers for different nodes will lead to unexpected behavior, as `treelib` uses them internally for lookup and maintaining structure. This is often confused with the `tag` attribute, which can be duplicated.
fix
Always provide a unique `identifier` when calling methods like `tree.create_node()`. If you need multiple nodes to share the same display text, use different identifiers for each node while keeping their `tag` attribute the same.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'treelib'
The treelib package is not installed in the current Python environment.
fix
Install the library using pip: `pip install treelib`
KeyError: 'parent_tag: some_tag' not found in the tree
The parent_tag specified for Tree.add_node() does not correspond to an existing node's identifier in the tree, or parent_tag is None for a node that is not the root.
fix
Ensure the parent_tag refers to an existing node's identifier, or set it to None only when adding the very first (root) node.
AttributeError: 'NoneType' object has no attribute 'tag'
A Node object was not found using tree.get_node(), which returned None, and subsequent code attempted to access an attribute (like 'tag') on this None object.
fix
Always check if the result of `tree.get_node()` is not `None` before attempting to access its attributes, or handle the case where the node might not exist.
FileNotFoundError: [Errno 2] No such file or directory: 'dot'
The Graphviz command-line tools, specifically the 'dot' executable, are not installed or not accessible in the system's PATH, which treelib requires for the export_dot() method.
fix
Install the Graphviz command-line tools for your operating system (e.g., `sudo apt-get install graphviz` on Debian/Ubuntu, `brew install graphviz` on macOS) and ensure its executable is in your system's PATH.
treelib.tree.NodeIDAbsentError: Parent identifier 'some_non_existent_id' not found.
When calling `tree.add_node()`, the provided `parent` identifier does not correspond to an existing node in the tree.
fix
Ensure that the parent node exists in the tree *before* attempting to add a child node to it, or specify `parent=None` for the root node.
Upgrade
Version history
1.8.0latest on PyPI · released Jun 29, 2025
Audit
Dependencies
graphvizoptionalRequired for `Tree.to_graphviz()` and other visualization methods. The Graphviz system utility must also be installed.
Agent activity
24 hits · last 30 days
node
20
OpenAI (training)
1
Resources