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

On the path of learning networks, there is a Networkx Error: Attributeerror encountered  when you view the nodes :
attribute error: ‘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

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

appears due to the lower version or old version of networks having the node attribute, and the higher or updated version of networks does not use the node attributes.

For solving this you need  some changes

Correction method 1:

By changing the node attributes 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: another method for solving this you should reinstall the lower version of networks.
PIP install: PIP install Networkx = = 2.3

Also Read: https://coderscanteen.com/ubuntu-tab-error-_complete96-bad-math-expression/

Ranjeet Singh Rawat
Ranjeet Singh Rawat

Leave a Reply

Your email address will not be published.

fifteen − 9 =