Anytree

raw JSON →
2.13.0 verified Wed May 13 auth: no python install: verified

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
error NameError: name 'findall' is not defined
cause The 'findall' function from the 'anytree' library was used without being imported.
fix
Import the function using 'from anytree.search import findall'.
error anytree.node.exceptions.TreeError: Parent node '11018432' is not of type 'NodeMixin'.
cause 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.
error NameError: name 'LevelOrderIter' is not defined
cause The 'LevelOrderIter' class from the 'anytree' library was used without being imported.
fix
Import the class using 'from anytree.iterators import LevelOrderIter'.
error AttributeError: 'RenderTree' object has no attribute 'name'
cause Passed a 'RenderTree' object instead of a 'Node' object to 'RenderTreeGraph'.
fix
Pass the root node directly to 'RenderTreeGraph', not the 'RenderTree' object.
error ModuleNotFoundError: No module named 'anytree'
cause 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
breaking Python 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.
breaking The `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`.
gotcha Prior 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.
gotcha To 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`).
gotcha The `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.
python os / libc status wheel install import disk mem side effects
3.10 alpine (musl) wheel - 0.01s 18.2M 0.4M clean
3.10 alpine (musl) - - 0.01s 18.2M 0.4M -
3.10 slim (glibc) wheel 1.5s 0.01s 19M 0.4M clean
3.10 slim (glibc) - - 0.01s 19M 0.4M -
3.11 alpine (musl) wheel - 0.01s 20.1M 0.4M clean
3.11 alpine (musl) - - 0.02s 20.1M 0.4M -
3.11 slim (glibc) wheel 1.6s 0.01s 21M 0.4M clean
3.11 slim (glibc) - - 0.01s 21M 0.4M -
3.12 alpine (musl) wheel - 0.01s 12.0M 0.4M clean
3.12 alpine (musl) - - 0.01s 12.0M 0.4M -
3.12 slim (glibc) wheel 1.4s 0.01s 12M 0.4M clean
3.12 slim (glibc) - - 0.01s 12M 0.4M -
3.13 alpine (musl) wheel - 0.01s 11.7M 0.6M clean
3.13 alpine (musl) - - 0.01s 11.6M 0.6M -
3.13 slim (glibc) wheel 1.4s 0.01s 12M 0.4M clean
3.13 slim (glibc) - - 0.01s 12M 0.4M -
3.9 alpine (musl) wheel - 0.01s 17.7M 0.4M clean
3.9 alpine (musl) - - 0.01s 17.7M 0.4M -
3.9 slim (glibc) wheel 1.7s 0.01s 18M 0.4M clean
3.9 slim (glibc) - - 0.01s 18M 0.4M -

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}")