Registry / data / leidenalg

leidenalg

JSON →
library0.12.0pypypi✓ verified 85d ago

leidenalg is a Python library that implements the Leiden algorithm for community detection in large networks. It is an extension of the Louvain algorithm, guaranteeing well-connected communities and often running faster. The library, currently at version 0.11.0, is actively maintained and provides Python bindings to a C++ core. It primarily relies on `python-igraph` for graph manipulation and can handle graphs with millions of nodes.

pip install leidenalg
INSTALL
IMPORT
SIG · LEIDENALG
L
leidenalg
datapythonv0.12.0
Install
2.1s avg
Import
109ms
Disk
43MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.12.0 · 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.116s · 47.3MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.1s · import 0.103s · 43MB
43MB installed
● package 43MB
Code
Verified usage

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

leidenalg
import leidenalg as la
Commonly aliased as 'la' in examples and documentation.
igraph
import igraph as ig
Required for creating and manipulating graph objects that leidenalg operates on. Commonly aliased as 'ig'.
ModularityVertexPartition
from leidenalg import ModularityVertexPartition
import leidenalg.ModularityVertexPartition
Partition types are classes within the leidenalg module and are directly imported for clarity when used as arguments to `find_partition`.

This quickstart demonstrates how to create a simple graph using `igraph` and then apply the Leiden algorithm using `leidenalg.find_partition` with the Modularity quality function to detect communities.

import igraph as ig import leidenalg as la # Create a sample graph (e.g., Erdos-Renyi graph) G = ig.Graph.Erdos_Renyi(100, 0.1) # Find a partition using the Leiden algorithm with Modularity partition = la.find_partition(G, la.ModularityVertexPartition) print(f"Number of nodes: {G.vcount()}") print(f"Number of edges: {G.ecount()}") print(f"Number of communities: {len(partition)}") print(f"Modularity: {partition.modularity}") # To get community membership for each node: # print(partition.membership)
Debug
Known issues
breakingPython 3.7 and 3.8 are no longer supported. Ensure you are using Python 3.9 or newer.
fix
Upgrade Python environment to 3.9 or higher. (e.g., `conda create -n myenv python=3.9`)
affects: <=0.10.x
gotchaThe default `resolution_parameter` (γ = 1) in `leidenalg` might not be optimal for all networks, potentially leading to an inappropriate number of communities.
fix
Experiment with different `resolution_parameter` values (e.g., between 0.4 and 2.0, or plot modularity/number of communities against resolution) to find a suitable partitioning for your specific data. Pass it as a keyword argument to the partition type, e.g., `la.find_partition(G, la.CPMVertexPartition, resolution_parameter=0.5)`.
affects: All versions
gotchaDirect installation with `pip install leidenalg` usually works due to binary wheels. However, if building from source, `leidenalg` depends on the C++ core libraries `igraph` (>= 1.0.0) and `libleidenalg` (>= 0.12), which may require manual compilation and specific build tools (e.g., `build-essential`, `autoconf`, `automake`, `flex`, `bison` on Ubuntu) if pre-built binaries are not available or suitable.
fix
For source installation, refer to the official documentation for detailed instructions on compiling the underlying C++ libraries. Using `conda-forge` for installation often handles these dependencies automatically.
affects: All versions (when installing from source)
gotchaWhen integrating with other libraries like `Scanpy`, ensure the graph object is correctly prepared before calling the Leiden algorithm. Leiden operates on graph structures, not raw data matrices.
fix
For `Scanpy`, run `sc.pp.neighbors()` to construct a k-nearest-neighbor graph before `sc.tl.leiden()`. Generally, ensure your data is converted to an `igraph.Graph` object.
affects: All versions
deprecatedThe `Scanpy` library, which often uses `leidenalg`, is shifting its default backend for Leiden clustering towards `igraph`'s internal implementation instead of `leidenalg`'s. While `leidenalg` is still the default for now, direct reliance on `leidenalg` via `Scanpy`'s API might change in future `Scanpy` versions.
fix
When using `Scanpy`, specify `flavor="igraph"` and `n_iterations=2` in `sc.tl.leiden()` to align with future defaults. For direct `leidenalg` usage, continue as normal.
affects: Scanpy >= 1.12.0 (future defaults)
Errors
Common errors & fixes
fatal error: igraph/igraph.h: No such file or directory
This error occurs during installation (e.g., `pip install leidenalg`) because the `leidenalg` Python package has a C++ core that depends on the `igraph` C core library, and the compiler cannot find the necessary `igraph.h` header file to build the Python bindings.
fix
Ensure that the `igraph` C core library and C++ build tools (like `g++`) are correctly installed and accessible on your system's PATH. For Debian/Ubuntu, use `sudo apt-get install build-essential libigraph-dev`. For other systems, consult the `igraph` C library installation guide. Afterward, try `pip install leidenalg` again.
ModuleNotFoundError: No module named 'leidenalg'
This error typically means the `leidenalg` package is not installed in the currently active Python environment, or the environment where it was installed is not being used by your script or application (e.g., when using R's `reticulate`, a different virtual environment, or an IDE that uses its own Python interpreter).
fix
Activate the correct Python environment where `leidenalg` is installed. If it's not installed, use `pip install leidenalg` or `conda install -c conda-forge leidenalg`. If using `reticulate` in R, explicitly set the Python environment using `reticulate::use_python()` or `reticulate::use_condaenv()`.
ImportError: DLL load failed while importing _c_leiden: The specified procedure could not be found.
This runtime import error, particularly common on Windows, indicates that a shared library (`DLL`) required by the `leidenalg` C++ extension (`_c_leiden`) could not be loaded. This often stems from an incompatibility between `leidenalg`'s compiled components and the installed `python-igraph` or other underlying C++ libraries, or missing runtime dependencies in the system's PATH.
fix
Try reinstalling `leidenalg` and `igraph` in a clean virtual environment, ensuring compatible versions. Using `conda install -c conda-forge leidenalg` is often more robust on Windows as it handles binary dependencies better. Ensure your system's PATH includes directories for any necessary runtime libraries.
AttributeError: 'Graph' object has no attribute 'vcount'
This error occurs when you attempt to use a graph object created with a different library (e.g., `networkx`) directly with `leidenalg`'s functions. `leidenalg` is designed to work with `igraph` graph objects, and `networkx` graphs do not have the `vcount` attribute that `igraph` graphs possess.
fix
Convert your `networkx` graph to an `igraph` graph object before passing it to `leidenalg` functions. You can do this using `igraph.Graph.from_networkx(your_networkx_graph)` (after `import igraph as ig`).
BaseException: Could not construct partition: Weight vector not the same size as the number of edges.
This error typically arises when the `weights` parameter provided to `leidenalg.find_partition` (often indirectly via a high-level library like `scanpy`) is a list or array whose length does not match the actual number of edges in the graph. This can happen if the weights are misaligned with the graph structure or if the graph itself was constructed incorrectly.
fix
Ensure that the `weights` array or list you pass to the `find_partition` function has the exact same number of elements as there are edges in your `igraph` graph object. If you're using `scanpy`, verify that `sc.pp.neighbors()` has correctly generated connectivities and that the weights are being accessed or passed appropriately.
Upgrade
Version history
0.12.0latest on PyPI · released May 24, 2026
Audit
Dependencies
python-igraphrequiredleidenalg relies on python-igraph for graph data structures and functionality.
Agent activity
2 hits · last 30 days
node
2
Resources
leidenalg — pip install leidenalg · libregistry