Tag Archives: graph

[Solved] Networkx Error: Attributeerror: ‘graph’ object has no attribute ‘node’

When learning Networkx, you encounter an error when viewing node attributes:
attributeerror: ‘graph’ object has no attribute ‘node’

G= nx.Graph(name='undirected graph',data=2022) # Create undirected graph
G.add_nodes_from([1,2,3,4,5]) # Add nodes to the graph using the list
G.add_node(6,name='F',weight=12)
print(G.node[6]['name']) # Check the other attributes of the node according to its ID

The reason is that the lower version of Networkx has the node attribute, while the higher version does not use the node attribute
correction method 1: just change the node attribute to nodes
the correct code is as follows:

G= nx.Graph(name='undirected graph',data=2022) # Create undirected graph
G.add_nodes_from([1,2,3,4,5]) # Add nodes to the graph using the list
G.add_node(6,name='F',weight=12)
print(G.nodes[6]['name']) # Check the other attributes of the node based on its ID

Correction 2: reinstall the lower version of Networkx
PIP install: PIP install Networkx = = 2.3