Powerful and Lightweight Python Tree Data Structure with various plugins
Project Links
Meta
Author: c0fec0de
Requires Python: <4.0,>=3.9.2
Classifiers
Topic
- Software Development :: Libraries :: Python Modules
Development Status
- 5 - Production/Stable
Programming Language
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
Links
If you enjoy anytree

Getting started
Usage is simple.
Construction
>>> from anytree import Node, RenderTree >>> 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)
Node
>>> print(udo) Node('/Udo') >>> print(joe) Node('/Udo/Dan/Joe')
Tree
>>> for pre, fill, node in RenderTree(udo): ... print("%s%s" % (pre, node.name)) Udo ├── Marc │ └── Lian └── Dan ├── Jet ├── Jan └── Joe
For details see Node and RenderTree.
Visualization
>>> from anytree.exporter import UniqueDotExporter >>> # graphviz needs to be installed for the next line! >>> UniqueDotExporter(udo).to_picture("udo.png")

The UniqueDotExporter can be started at any node and has various formatting hookups:
>>> UniqueDotExporter(dan, ... nodeattrfunc=lambda node: "fixedsize=true, width=1, height=1, shape=diamond", ... edgeattrfunc=lambda parent, child: "style=bold" ... ).to_picture("dan.png")

There are various other Importers and Exporters.
Manipulation
A second tree:
>>> mary = Node("Mary") >>> urs = Node("Urs", parent=mary) >>> chris = Node("Chris", parent=mary) >>> marta = Node("Marta", parent=mary) >>> print(RenderTree(mary)) Node('/Mary') ├── Node('/Mary/Urs') ├── Node('/Mary/Chris') └── Node('/Mary/Marta')
Append:
>>> udo.parent = mary >>> print(RenderTree(mary)) Node('/Mary') ├── Node('/Mary/Urs') ├── Node('/Mary/Chris') ├── Node('/Mary/Marta') └── Node('/Mary/Udo') ├── Node('/Mary/Udo/Marc') │ └── Node('/Mary/Udo/Marc/Lian') └── Node('/Mary/Udo/Dan') ├── Node('/Mary/Udo/Dan/Jet') ├── Node('/Mary/Udo/Dan/Jan') └── Node('/Mary/Udo/Dan/Joe')
Subtree rendering:
>>> print(RenderTree(marc)) Node('/Mary/Udo/Marc') └── Node('/Mary/Udo/Marc/Lian')
Cut:
>>> dan.parent = None >>> print(RenderTree(dan)) Node('/Dan') ├── Node('/Dan/Jet') ├── Node('/Dan/Jan') └── Node('/Dan/Joe')
Extending any python class to become a tree node
The entire tree magic is encapsulated by NodeMixin add it as base class and the class becomes a tree node:
>>> from anytree import NodeMixin, RenderTree >>> class MyBaseClass(object): # Just an example of a base class ... foo = 4 >>> class MyClass(MyBaseClass, NodeMixin): # Add Node feature ... def __init__(self, name, length, width, parent=None, children=None): ... super(MyClass, self).__init__() ... self.name = name ... self.length = length ... self.width = width ... self.parent = parent ... if children: ... self.children = children
Just set the parent attribute to reflect the tree relation:
>>> my0 = MyClass('my0', 0, 0) >>> my1 = MyClass('my1', 1, 0, parent=my0) >>> my2 = MyClass('my2', 0, 2, parent=my0)
>>> for pre, fill, node in RenderTree(my0): ... treestr = u"%s%s" % (pre, node.name) ... print(treestr.ljust(8), node.length, node.width) my0 0 0 ├── my1 1 0 └── my2 0 2
The children can be used likewise:
>>> my0 = MyClass('my0', 0, 0, children=[ ... MyClass('my1', 1, 0), ... MyClass('my2', 0, 2), ... ])
>>> for pre, fill, node in RenderTree(my0): ... treestr = u"%s%s" % (pre, node.name) ... print(treestr.ljust(8), node.length, node.width) my0 0 0 ├── my1 1 0 └── my2 0 2
Documentation
Please see the Documentation for all details.
Installation
To install the anytree module run:
pip install anytree
If you do not have write-permissions to the python installation, try:
pip install anytree --user
Apr 08, 2025
2.13.0
Nov 16, 2023
2.12.1
Oct 26, 2023
2.12.0
Oct 26, 2023
2.11.2
Oct 19, 2023
2.11.1
Oct 19, 2023
2.11.0
Oct 11, 2023
2.10.0
Oct 11, 2023
2.9.2
Oct 11, 2023
2.9.1
Jun 25, 2023
2.9.0
Jan 15, 2020
2.8.0
Dec 08, 2019
2.7.3
Oct 10, 2019
2.7.2
Sep 29, 2019
2.7.1
Sep 24, 2019
2.7.0
Feb 09, 2019
2.6.0
Jan 30, 2019
2.5.0
Dec 13, 2017
2.4.3
Nov 28, 2017
2.4.2
Nov 26, 2017
2.4.1
Nov 26, 2017
2.4.0
Nov 25, 2017
2.3.0
Nov 06, 2017
2.2.2
Jun 06, 2017
2.2.1
May 23, 2017
2.2.0
May 03, 2017
2.1.4
Apr 12, 2017
2.1.3
Mar 30, 2017
2.1.2
Mar 27, 2017
2.1.1
Mar 24, 2017
2.1.0
Mar 13, 2017
2.0.0
Mar 10, 2017
1.0.4
Mar 10, 2017
1.0.2
Sep 04, 2016
1.0.1
Sep 03, 2016
1.0.0
Sep 02, 2016
0.0.1
Wheel compatibility matrix
Files in release
No dependencies