Registry / ai-ml / python-louvain

python-louvain

JSON →
library0.16pypypiunverified

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 networkx
INSTALL
IMPORT
SIG · PYTHON-LOUVAIN
P
python-louvain
ai-mlpythonv0.16
Install
5.5s avg
Import
651ms
Disk
106MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.16 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.655s · 105MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 5.5s · import 0.646s · 101MB
106MB installed
● package 106MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

community_louvain
from community import community_louvain
import python_louvain
The PyPI package is named 'python-louvain', but its main module is imported as 'community'. [4, 11]
best_partition
partition = community_louvain.best_partition(G)
partition = community.best_partition(G)
While 'import community' might work, explicitly importing 'community_louvain' is clearer and avoids potential conflicts with other 'community' packages. [11]

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.

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)
Debug
Known issues
gotchaThe 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.
fix
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]
affects: All versions
gotchaThe 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]
fix
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]
affects: All versions
gotchaThe `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]
fix
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]
affects: All versions
Upgrade
Version history
0.16latest on PyPI · released Jan 29, 2022
Audit
Dependencies
networkxrequiredpython-louvain is built on top of the NetworkX framework for graph representation and manipulation. While NetworkX 1.11 is mentioned in older docs, current versions are generally compatible. [1, 4]
Agent activity
17 hits · last 30 days
node
16
OpenAI (training)
1
Resources
python-louvain — pip install python-louvain · libregistry