{"id":7629,"library":"python-louvain","title":"Python Louvain Community Detection","description":"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.","status":"maintenance","version":"0.16","language":"en","source_language":"en","source_url":"https://github.com/taynaud/python-louvain","tags":["community detection","graph algorithms","networkx","louvain","modularity"],"install":[{"cmd":"pip install python-louvain networkx","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"python-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]","package":"networkx"}],"imports":[{"note":"The PyPI package is named 'python-louvain', but its main module is imported as 'community'. [4, 11]","wrong":"import python_louvain","symbol":"community_louvain","correct":"from community import community_louvain"},{"note":"While 'import community' might work, explicitly importing 'community_louvain' is clearer and avoids potential conflicts with other 'community' packages. [11]","wrong":"partition = community.best_partition(G)","symbol":"best_partition","correct":"partition = community_louvain.best_partition(G)"}],"quickstart":{"code":"import networkx as nx\nfrom community import community_louvain\n\n# Create a sample graph\nG = nx.Graph()\nG.add_edges_from([(0, 1), (0, 2), (1, 2), (3, 4), (3, 5), (4, 5), (0, 3)])\n\n# Compute the best partition using the Louvain method\npartition = community_louvain.best_partition(G)\n\nprint(\"Graph Nodes:\", G.nodes())\nprint(\"Detected Communities:\", partition)\n\n# Example of how to get the modularity of the partition\nmodularity = community_louvain.modularity(partition, G)\nprint(\"Modularity:\", modularity)","lang":"python","description":"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."},"warnings":[{"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]","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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]","message":"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]","severity":"gotcha","affected_versions":"All versions"},{"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]","message":"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]","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"The correct import path is `from community import community_louvain` or `import community`. [4, 11]","cause":"The Python package `python-louvain` is installed, but the import statement uses the PyPI package name directly instead of its internal module name.","error":"ModuleNotFoundError: No module named 'python_louvain'"},{"fix":"Verify 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]","cause":"This typically occurs when a different package also named 'community' is installed, shadowing the `community` module provided by `python-louvain`. [11]","error":"AttributeError: module 'community' has no attribute 'best_partition'"},{"fix":"Ensure 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]","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]","error":"ERROR: Command errored out with exit status 1: ... Could not compile the C core of igraph."}]}