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.
pip install python-louvain networkxVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a simple NetworkX graph and then apply the Louvain algorithm using `community_louvain.best_partition` to find community assignments. It also shows how to calculate the modularity of the resulting partition.
Always use `from community import community_louvain` or `import community` after `pip install python-louvain`. Ensure no other packages named 'community' are conflicting in your environment. [4, 11, 16]
For reproducible results, use the `randomize=False` parameter in `best_partition` (if available in your version) or set a random seed for NumPy and Python's random module before graph creation or partition calculation, though direct control over the Louvain algorithm's internal randomness for reproducibility can be limited. [18]
If working with directed graphs where directionality is crucial for community detection, consider converting your graph to an undirected graph if appropriate for your analysis, or explore specialized algorithms for directed community detection (e.g., `DirectedLouvain` mentioned by users). [24]