4

The problem I'm trying to solve is the following: I have a networkxgraph that I would like to draw as a circular tree. This requires Graphviz and either PyGraphviz or Pydot, which are not available for Python 3.

I'm considering the option of serialising the graph-object in python 3, and then de-serialising it in a python 2 environment where I could draw it. It's, however, unclear to me what the potential issues are with this type of approach since I cannot assume that the respective objects are similar in both environments.

Apart from actually trying this out (I currently have no python 2 environment on my machine), is there any way to find out if this is possible? Or would it simply make more sense to follow a different route by either exporting to a different format (e.g. gephy), and perhaps even doing the drawing outside of python?

Patrick Allo
  • 473
  • 4
  • 15
  • 1
    the best way would be to make your object state compatible with standard python structures and use `json` to serialize. No surprises. – Jean-François Fabre Mar 22 '17 at 12:24
  • Upgrading a project to Python 3 is *not that hard anymore*. Consider creating a pull request for either PyGraphviz or Pydot to add support? – Martijn Pieters Mar 22 '17 at 12:25
  • The reverse question: http://stackoverflow.com/questions/28218466/unpickling-a-python-2-object-with-python-3 – Chris_Rands Mar 22 '17 at 12:26
  • `graphviz` is available for Python 3 though – Chris_Rands Mar 22 '17 at 12:28
  • json definitely seems to be an option, as explained in this [question](http://stackoverflow.com/questions/3162909/method-to-save-networkx-graph-to-json-graph) – Patrick Allo Mar 22 '17 at 12:29
  • Actually, `PyDot` claims to support Python 3, did you actually try using it? – Martijn Pieters Mar 22 '17 at 12:36
  • I didn't try "using" pydot, but did try to install it. At least on anaconda it leads to the following error: UnsatisfiableError: The following specifications were found to be in conflict: - pydot -> pyparsing 1.5.6 -> python 2.7* - python 3.5* – Patrick Allo Mar 22 '17 at 12:38
  • Oh, and PyGraphviz supports Python too. – Martijn Pieters Mar 22 '17 at 12:39
  • @PatrickAllo: it sounds as if you already have Pyparsing 1.5.6 installed, Pydot requires 2.1.4 or newer. – Martijn Pieters Mar 22 '17 at 12:49
  • @MartijnPieters: After some more fiddling (I did already have Pyparsing 2.1.4) I have all the required modules installed. The last bit was to notice that there was a mistake in this online example: https://networkx.github.io/documentation/networkx-1.9/examples/drawing/circular_tree.html – Patrick Allo Mar 22 '17 at 14:57

1 Answers1

2

Yes, you can dump data in Python 3 to load it again in Python 2. You probably want to set fix_imports, as well as use a protocol lower than 3. From the pickle.dump() documenation:

If fix_imports is true and protocol is less than 3, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.

Protocol version 2 is supported in Python 2.3 - 2.7.

However, consider some of the alternatives:

  • There are three Graphviz packages that support Python 3:

  • Use a different serialisation format. JSON should be able to handle a graph just fine.

Aric
  • 24,511
  • 5
  • 78
  • 77
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343