PyMetis
raw JSON → 2025.2.2 verified Mon Apr 27 auth: no python
PyMetis is a Python wrapper for METIS, a set of serial graph partitioning algorithms. It provides both low-level and high-level interfaces for partitioning graphs. Version 2025.2.2 supports Python >=3.10. The package is actively maintained with periodic releases.
pip install pymetis Common errors
error ImportError: No module named pymetis ↓
cause pymetis is not installed or Python version is <3.10.
fix
Install pymetis with
pip install pymetis or upgrade Python. error ValueError: adjacency must be a list of lists of integers ↓
cause Adjacency list contains non-integer elements (e.g., floats) or is not a list of lists.
fix
Ensure adjacency is a list of lists of ints, e.g.,
[[1,2],[0,2]]. error pymetis.part_graph returns only one argument (unexpected) ↓
cause Using older version where part_graph returned a single value; modern pymetis returns a tuple (cut, parts).
fix
Update code to unpack two values: cut, parts = pymetis.part_graph(...)
Warnings
gotcha METIS requires that graph adjacency lists contain no duplicate edges and no self-loops. pymetis does not validate inputs; passing invalid graphs can cause silent errors or crashes. ↓
fix Preprocess your graph to remove duplicates and self-loops before calling pymetis.
breaking In v2025.2, the zero-copy mode was introduced. If you rely on adjacency being modified by the C code (e.g., reordering), note that zero-copy may change behavior. The option `zero_copy=True` can be passed to `part_graph` to disable copying. ↓
fix Use `zero_copy=False` if you need the old behavior of copying the adjacency list.
gotcha METIS functions can segfault if the graph is too large or if memory is insufficient. pymetis does not provide graceful error handling for such cases. ↓
fix Ensure sufficient memory and consider using iterative partitioning for large graphs.
Imports
- pymetis
import pymetis - pymetis.part_graph wrong
from pymetis import part_graphcorrectimport pymetis part = pymetis.part_graph(nparts, adjacency=adjacency)
Quickstart
import numpy as np
import pymetis
# Define adjacency list for a small graph
# Each entry is a list of neighbor indices
adjacency = [[1, 2], [0, 2], [0, 1, 3], [2]]
nparts = 2
cut, parts = pymetis.part_graph(nparts, adjacency=adjacency)
print(f"Edge cut: {cut}")
print(f"Partition assignments: {parts}")