Registry / data / anytree

anytree

JSON →
library2.13.0pypypi✓ verified 25d ago

Anytree is a powerful and lightweight Python library that provides a tree data structure with various plugins. It simplifies the creation, manipulation, and visualization of hierarchical data. The library is actively maintained with regular releases, approximately every few months, and is currently at version 2.13.0.

pip install anytree
INSTALL
IMPORT
SIG · ANYTREE
A
anytree
datapythonv2.13.0
Install
1.6s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.13.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.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 18.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.010s · 19MB
16MB installed
● package 16MB
Code
Verified usage

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

Node
from anytree import Node
AnyNode
from anytree import AnyNode
NodeMixin
from anytree import NodeMixin
RenderTree
from anytree import RenderTree
DotExporter
from anytree.exporter import DotExporter
LightNodeMixin
from anytree import LightNodeMixin
from anytree.node.lightnodemixin import LightNodeMixin
While importing from the submodule works, LightNodeMixin is typically made available directly from the top-level 'anytree' package since its introduction in 2.12.0 for convenience.

This quickstart demonstrates how to create a tree using `anytree.Node` objects, either by assigning parents incrementally or by defining the full hierarchy with the `children` keyword. It then uses `RenderTree` to print a visual representation of the tree structure to the console.

from anytree import Node, RenderTree # Create nodes by specifying parent udo = Node("Udo") marc = Node("Marc", parent=udo) lian = Node("Lian", parent=marc) dan = Node("Dan", parent=udo) jet = Node("Jet", parent=dan) jan = Node("Jan", parent=dan) joe = Node("Joe", parent=dan) # Or create a complex tree structure directly using children keyword root_complex = Node("root", children=[ Node("sub0", children=[ Node("sub0B", foo=4, bar=109), Node("sub0A") ]), Node("sub1", children=[ Node("sub1A"), Node("sub1B", bar=8), Node("sub1C", children=[ Node("sub1Ca") ]) ]) ]) # Render the tree for pre, fill, node in RenderTree(root_complex): print(f"{pre}{node.name}")
Debug
Known issues
breakingPython 2.x support was removed in version 2.9.0. Users upgrading from versions prior to 2.9.0 must migrate to Python 3.x.
fix
Upgrade to Python 3.9.2 or newer, and adjust code for Python 3 compatibility if necessary.
affects: <2.9.0
breakingThe `anchestors` attribute, which was a typo for `ancestors`, is deprecated and will be removed in version 3.0.0.
fix
Replace all usages of `node.anchestors` with `node.ancestors`.
affects: >=2.9.0
gotchaPrior to version 2.12.0, unpickling a tree containing a `SymlinkNode` could lead to a `RecursionError` due to issues in `__getattr__` for `SymlinkNodeMixin`.
fix
Upgrade to anytree version 2.12.0 or newer to resolve this issue. If upgrading is not possible, avoid pickling trees with `SymlinkNode` instances.
affects: <2.12.0
gotchaTo export trees as images (e.g., PNG, DOT) using exporters like `DotExporter`, you must install the `graphviz` Python package (`pip install graphviz`) and the underlying `Graphviz` system tool separately.
fix
Install `pip install graphviz` and ensure the Graphviz system utility is installed and available in your system's PATH (e.g., `apt-get install graphviz` or `brew install graphviz`).
affects: All versions (where export functionality is used)
gotchaThe `LightNodeMixin` (introduced in 2.12.0) behaves similarly to `NodeMixin` but uses `__slots__` for potentially better memory efficiency. This means it has minor differences in object behavior, such as not allowing arbitrary new attributes to be added dynamically after initialization.
fix
Be aware of the implications of `__slots__` when using `LightNodeMixin` and prefer to pre-define all attributes or use `NodeMixin` if dynamic attribute assignment is critical.
affects: >=2.12.0
Errors
Common errors & fixes
NameError: name 'findall' is not defined
The 'findall' function from the 'anytree' library was used without being imported.
fix
Import the function using 'from anytree.search import findall'.
anytree.node.exceptions.TreeError: Parent node '11018432' is not of type 'NodeMixin'.
Attempted to set a parent node using a string instead of a 'NodeMixin' instance.
fix
Ensure that the parent parameter is a 'Node' object, not a string.
NameError: name 'LevelOrderIter' is not defined
The 'LevelOrderIter' class from the 'anytree' library was used without being imported.
fix
Import the class using 'from anytree.iterators import LevelOrderIter'.
AttributeError: 'RenderTree' object has no attribute 'name'
Passed a 'RenderTree' object instead of a 'Node' object to 'RenderTreeGraph'.
fix
Pass the root node directly to 'RenderTreeGraph', not the 'RenderTree' object.
ModuleNotFoundError: No module named 'anytree'
The 'anytree' library is not installed in the Python environment where the code is being executed.
fix
Install the anytree library using pip: `pip install anytree`
Upgrade
Version history
2.13.0latest on PyPI · released Apr 8, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
53 hits · last 30 days
node
46
OpenAI (training)
1
Resources
anytree — pip install anytree · libregistry