newick
raw JSON → 1.11.0 verified Fri May 01 auth: no python
A Python module to read and write the Newick format, commonly used for phylogenetic trees. Current version: 1.11.0. Release cadence is irregular; maintained on GitHub.
pip install newick Common errors
error AttributeError: module 'newick' has no attribute 'loads' ↓
cause Installed old version (<1.0) that lacked loads/dumps; or named conflict with other module.
fix
Upgrade to latest: pip install --upgrade newick. Check import: from newick import loads
error IndexError: list index out of range when accessing tree[0] ↓
cause The Newick string is empty or malformed (e.g., missing semicolon). loads() returns empty list.
fix
Ensure the Newick string is valid and ends with ';'. Check: tree = loads(newick_str); assert tree
error TypeError: expects a string or file-like object ↓
cause Passed a Node object to load() instead of a file path or string.
fix
Use dumps(node) to serialize, not load(). load() is for reading from file.
error ValueError: Node name contains illegal characters ↓
cause Node name includes unescaped newick special characters like '(', ')', ':', ',', ';'.
fix
Quote the name using single quotes in the Newick string, e.g., "'My:Node'".
Warnings
gotcha loads() returns a list of Node objects, not a single Node. For a tree with one root, access index 0. ↓
fix tree = loads(newick_str)[0]
gotcha Node names can include special characters like ':' or ',' but must be quoted. The library may fail to parse if names contain unescaped special characters. ↓
fix Ensure node names are properly escaped or quoted in the Newick string.
deprecated The 'read' and 'write' functions (from newick import read, write) are deprecated as of v1.9.0. Use load/dump instead. ↓
fix Use load() for reading files and dump() for writing.
breaking In v1.9.0, the internal Node class changed: removed 'ancestor' attribute and added 'parent'. Code relying on 'node.ancestor' will break. ↓
fix Use 'node.parent' instead of 'node.ancestor'.
Imports
- loads
from newick import loads - dumps
from newick import dumps - load
from newick import load - dump
from newick import dump
Quickstart
from newick import loads, dumps
# Load a Newick string
tree = loads('(A:0.1,B:0.2,(C:0.3,D:0.4):0.5);')
# tree is a list of Node objects (rooted at index 0)
node = tree[0]
print(node.name) # None (internal node)
print(node.descendants) # list of Nodes
print(node.length) # None (for root)
# Modify the tree and dump back to string
node.name = 'root'
newick_str = dumps(node)
print(newick_str)