'pickling networkx graph: can't pickle generator objects

I am trying to pickle my networkx graph but getting the next error

can't pickle generator objects

I read TypeError: can't pickle generator objects that you can't pickle generator. how can I find where is my generator in the graph object ? is there a way to traverse recursively on object and find type == generator ?



Solution 1:[1]

This is likely an issue with networkx version on Python 2.x, stemming from the change in networkx functions (e.g., for calculating shortest path lengths) that return a generator in the recent versions of the package as opposed to a dictionary in the 1.x versions.

A workaround would be checking whether the object returned by networkx is a generator and if it is, converting it to picklable object, for example the following code was tested using Python 2.7.16 and networkx 2.2:

import networkx, types, cPickle
G = networkx.cycle_graph(5)
val = networkx.shortest_path_length(G)
if isinstance(val, types.GeneratorType): # newer versions of networkx returns generator
    val_new = {source: target_dict for source, target_dict in val}
    val = val_new
cPickle.dump(val, open("test.pkl", 'w'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 emre