Python Louvain Community Detection
python-louvain is a Python implementation of the Louvain algorithm for community detection in large networks. It is built on top of the NetworkX framework. The current stable version is 0.16, released in January 2022. The project appears to be in a maintenance phase, with infrequent releases.
Common errors
-
ModuleNotFoundError: No module named 'python_louvain'
cause The Python package `python-louvain` is installed, but the import statement uses the PyPI package name directly instead of its internal module name.fixThe correct import path is `from community import community_louvain` or `import community`. [4, 11] -
AttributeError: module 'community' has no attribute 'best_partition'
cause This typically occurs when a different package also named 'community' is installed, shadowing the `community` module provided by `python-louvain`. [11]fixVerify that `python-louvain` is correctly installed. If other 'community' packages exist, explicitly import `community_louvain` with `from community import community_louvain` or manage your virtual environment to avoid conflicts. You might need to uninstall conflicting packages or inspect your `sys.path`. [11] -
ERROR: Command errored out with exit status 1: ... Could not compile the C core of igraph.
cause This error is common when attempting to install a *different* Louvain-related package (e.g., `louvain` which depends on `python-igraph` and its C core), not `python-louvain`. `python-louvain` relies on NetworkX and pure Python. [13, 21]fixEnsure you are installing `python-louvain` (which uses NetworkX) via `pip install python-louvain networkx`. If you intend to use the `louvain` package (which uses `python-igraph`), you will need to install `igraph`'s C core dependencies first, which varies by operating system. For `python-louvain`, this error is irrelevant. [13, 21]
Warnings
- gotcha The package name for installation is `python-louvain`, but the primary module to import is `community`. This often leads to `ModuleNotFoundError` or confusion if users try `import python_louvain` or if another 'community' package is installed.
- gotcha The Louvain algorithm is not strictly deterministic. Different runs on the same graph might yield slightly different partitions due to the order in which nodes are processed, especially in cases with multiple optimal solutions. [22]
- gotcha The `python-louvain` package primarily supports undirected graphs. While NetworkX can handle directed graphs, applying `best_partition` to a directed graph often treats it as undirected. [24]
Install
-
pip install python-louvain networkx
Imports
- community_louvain
import python_louvain
from community import community_louvain
- best_partition
partition = community.best_partition(G)
partition = community_louvain.best_partition(G)
Quickstart
import networkx as nx
from community import community_louvain
# Create a sample graph
G = nx.Graph()
G.add_edges_from([(0, 1), (0, 2), (1, 2), (3, 4), (3, 5), (4, 5), (0, 3)])
# Compute the best partition using the Louvain method
partition = community_louvain.best_partition(G)
print("Graph Nodes:", G.nodes())
print("Detected Communities:", partition)
# Example of how to get the modularity of the partition
modularity = community_louvain.modularity(partition, G)
print("Modularity:", modularity)