module 'networkx' has no attribute 'read_gpickle'
Asked Answered
M

2

6

I'm trying to run this code that uses networkx to read a graph pickle file.

def read_graph(self, path=f'./dblp_graph.gpickle'):
    self.g = networkx.read_gpickle(path=path)
    return self.g

When I run this code using the Jupyter notebook I got following error:

module 'networkx' has no attribute 'read_gpickle'

Then, using THIS document I test networkx.readwrite.gpickle.read_gpickle instead and got this error.

module 'networkx.readwrite' has no attribute 'gpickle'

I also tried:

print(dir(nx.readwrite))

and the result is:

['GraphMLReader', 'GraphMLWriter', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'adjacency', 'adjacency_data', 'adjacency_graph', 'adjlist', 'cytoscape', 'cytoscape_data', 'cytoscape_graph', 'edgelist', 'forest_str', 'from_graph6_bytes', 'from_sparse6_bytes', 'generate_adjlist', 'generate_edgelist', 'generate_gexf', 'generate_gml', 'generate_graphml', 'generate_multiline_adjlist', 'generate_network_text', 'generate_pajek', 'gexf', 'gml', 'graph6', 'graphml', 'json_graph', 'leda', 'multiline_adjlist', 'node_link', 'node_link_data', 'node_link_graph', 'pajek', 'parse_adjlist', 'parse_edgelist', 'parse_gml', 'parse_graphml', 'parse_leda', 'parse_multiline_adjlist', 'parse_pajek', 'read_adjlist', 'read_edgelist', 'read_gexf', 'read_gml', 'read_graph6', 'read_graphml', 'read_leda', 'read_multiline_adjlist', 'read_pajek', 'read_sparse6', 'read_weighted_edgelist', 'relabel_gexf_graph', 'sparse6', 'text', 'to_graph6_bytes', 'to_sparse6_bytes', 'tree', 'tree_data', 'tree_graph', 'write_adjlist', 'write_edgelist', 'write_gexf', 'write_gml', 'write_graph6', 'write_graphml', 'write_graphml_lxml', 'write_graphml_xml', 'write_multiline_adjlist', 'write_network_text', 'write_pajek', 'write_sparse6', 'write_weighted_edgelist']

So, there is no function to read/write pickle files!

P.s: I also forced jupyter to use networkx version 2.5 using "!pip3 install networkx==2.5" but there is no difference.

Menagerie answered 13/4, 2023 at 2:48 Comment(1)
read_gpickle and write_gpickle were removed in 3.0.Scumble
P
8

According to the Migration Guide from 2.x to 3.0:

The functions read_gpickle and write_gpickle were removed in 3.0. You can read and write NetworkX graphs as Python pickles.

import pickle
G = nx.path_graph(4)

with open('test.gpickle', 'wb') as f:
    pickle.dump(G, f, pickle.HIGHEST_PROTOCOL)

and to read a pickle:

with open('test.gpickle', 'rb') as f:
    G = pickle.load(f)
Pahari answered 27/10, 2023 at 22:31 Comment(0)
B
0

In networkx last version, this way to write graphs has dissapeared. https://networkx.org/documentation/stable/reference/readwrite/index.html# Here the actual options.

Baier answered 17/8, 2023 at 12:8 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Wolf

© 2022 - 2025 — McMap. All rights reserved.