Search code examples
pythonnetworkx

Can't import louvain_partitions from networkx


Python 3.9 on Mac OSX 11.6.1 with networkx 2.7.1 (latest release)

I'm unable to access the louvain_partitions tool from networkx to determine the communities in a simple un-directed network. While I have no problems creating graphs and doing other operations, I just can't import this method. Here's a simple example with the error message. I must be missing something obvious.

import numpy as np
import networkx as nx
A=np.array([[0,1,1,0,0],[1,0,1,0,0],[1,1,0,0,0],[0,0,0,0,1],[0,0,0,1,0]])
G=nx.from_numpy_matrix(A)
clusters=nx.louvain_partitions(G)

Traceback (most recent call last):   File "<pyshell#9>", line 1, in <module>
    clusters=nx.louvain_partitions(G)   File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/networkx/__init__.py", line 51, in __getattr__
    raise AttributeError(f"module {__name__} has no attribute {name}") AttributeError: module networkx has no attribute louvain_partitions

Solution

  • It is not directly under nx, this worked for me.

    clusters=nx.algorithms.community.louvain_partitions(G)