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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 18.2MB
glibcpy 3.10–3.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.fixUpgrade 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.fixReplace 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`.fixUpgrade 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.fixInstall `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.fixBe 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.
fixImport 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.
fixEnsure 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.
fixImport 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'.
fixPass 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.
fixInstall 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.