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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.116s · 47.3MB
glibcpy 3.10–3.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.fixUpgrade 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.fixExperiment 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.fixFor 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.fixFor `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.fixWhen 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.
fixEnsure 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).
fixActivate 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.
fixTry 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.
fixConvert 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.
fixEnsure 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.