简体   繁体   中英

module 'networkx' has no attribute 'add_nodes_from'

I have a dictionary with author name strings as indexes and the number of publications as the associated value. When I try adding nodes to a new graph from it, I get the following error:

AttributeError: module 'networkx' has no attribute 'add_nodes_from'

Here is a sample code:

import networkx as nx
auth_dict = {"albert": 1, "Barbie": 3, "Charlie": 8}
G = nx.MultiGraph()
G = nx.add_nodes_from(auth_dict)

Environment is pip managed python 3.7.2 with networkx 2.2, MacOS 10.13.6

This is the reference I tried to follow: https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.add_nodes_from.html#networkx.Graph.add_nodes_from

Thank you

You're calling add_nodes_from the wrong way. It is a method of the base MultiGraph class, and not an attribute of the networkx module itself. So the Syntax should be

G = nx.MultiGraph()
G.add_nodes_from(auth_dict)

(notice the dot instead of '=').

So i guess you're calling it as

G = nx.add_nodes_from(foo)

in your main code, which is, again, the wrong syntax - look here or at the link you posted yourself for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM