louvain

raw JSON →
0.8.2 verified Fri May 01 auth: no python

louvain is a Python implementation of the Louvain algorithm for community detection in large networks, based on the igraph library. Current version is 0.8.2, with a relatively stable release cadence (last update 2024).

pip install louvain
error ModuleNotFoundError: No module named 'igraph'
cause louvain requires igraph but doesn't install it as a dependency
fix
pip install python-igraph
error ImportError: cannot import name 'find_partition' from 'louvain'
cause Old API (v0.5.x) used 'best_partition'; newer versions use 'find_partition'
fix
Use: partition = louvain.find_partition(G, louvain.ModularityVertexPartition)
error AttributeError: module 'louvain' has no attribute 'best_partition'
cause The 'best_partition' function was removed after v0.5.x
fix
Use louvain.find_partition() with appropriate partition class
deprecated The standalone 'louvain' package is deprecated; consider using 'leidenalg' for newer algorithms
fix Use pip install leidenalg and import leidenalg (also by same author, more efficient)
gotcha Must install python-igraph separately
fix Run: pip install python-igraph
gotcha The 'louvain' module does not install igraph automatically; missing igraph causes ImportError
fix Ensure igraph is installed and imported before louvain

Basic community detection using the Louvain algorithm on a random graph.

import igraph as ig
import louvain

G = ig.Graph.Erdos_Renyi(n=100, p=0.1, directed=False)
partition = louvain.find_partition(G, louvain.ModularityVertexPartition)
print(partition.membership)