0

I wanted to draw a simple graph with networkx and graphviz but there seems to be a problem with pydot.

I'm using

  • Ubuntu 18.04.4 LTS
  • Python 3.6.9
  • graphviz 0.14
  • networkx 2.4
  • the newest versions of pydot, pydot2, pydot3 and pydotplus

I tried to run:

import networkx as nx

G = nx.complete_graph(4)
pos = nx.nx_pydot.graphviz_layout(G)
pos = nx.nx_pydot.graphviz_layout(G, prog='dot')

but seem to get an error when no matter what pydot module I use and I do not know why.

for pydot2 1.0.33 I get:

Couldn't import dot_parser, loading of dot files will not be possible.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in 
      1 G = nx.complete_graph(4)
----> 2 pos = nx.nx_pydot.graphviz_layout(G)
      3 pos = nx.nx_pydot.graphviz_layout(G, prog='dot')

/usr/local/lib/python3.6/dist-packages/networkx/drawing/nx_pydot.py in graphviz_layout(G, prog, root)
    266     This is a wrapper for pydot_layout.
    267     """
--> 268     return pydot_layout(G=G, prog=prog, root=root)
    269 
    270 

/usr/local/lib/python3.6/dist-packages/networkx/drawing/nx_pydot.py in pydot_layout(G, prog, root)
    314     # List of low-level bytes comprising a string in the dot language converted
    315     # from the passed graph with the passed external GraphViz command.
--> 316     D_bytes = P.create_dot(prog=prog)
    317 
    318     # Unique string decoded from these bytes with the preferred locale encoding

/usr/local/lib/python3.6/dist-packages/pydot.py in (f, prog)
   1800             self.__setattr__(
   1801                 'create_'+frmt,
-> 1802                 lambda f=frmt, prog=self.prog : self.create(format=f, prog=prog))
   1803             f = self.__dict__['create_'+frmt]
   1804             f.__doc__ = '''Refer to the docstring accompanying the 'create' method for more information.'''

/usr/local/lib/python3.6/dist-packages/pydot.py in create(self, prog, format)
   1964         tmp_fd, tmp_name = tempfile.mkstemp()
   1965         os.close(tmp_fd)
-> 1966         self.write(tmp_name)
   1967         tmp_dir = os.path.dirname(tmp_name )
   1968 

/usr/local/lib/python3.6/dist-packages/pydot.py in write(self, path, prog, format)
   1893             prog = self.prog
   1894 
-> 1895         dot_fd = file(path, "w+b")
   1896         if format == 'raw':
   1897             data = self.to_string()

NameError: name 'file' is not defined

for pydot3 I get:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
      1 G = nx.complete_graph(4)
----> 2 pos = nx.nx_pydot.graphviz_layout(G)
      3 pos = nx.nx_pydot.graphviz_layout(G, prog='dot')

/usr/local/lib/python3.6/dist-packages/networkx/drawing/nx_pydot.py in graphviz_layout(G, prog, root)
    266     This is a wrapper for pydot_layout.
    267     """
--> 268     return pydot_layout(G=G, prog=prog, root=root)
    269 
    270 

/usr/local/lib/python3.6/dist-packages/networkx/drawing/nx_pydot.py in pydot_layout(G, prog, root)
    314     # List of low-level bytes comprising a string in the dot language converted
    315     # from the passed graph with the passed external GraphViz command.
--> 316     D_bytes = P.create_dot(prog=prog)
    317 
    318     # Unique string decoded from these bytes with the preferred locale encoding

/usr/local/lib/python3.6/dist-packages/pydot/__init__.py in (f, prog)
   1798             self.__setattr__(
   1799                 'create_'+frmt,
-> 1800                 lambda f=frmt, prog=self.prog : self.create(format=f, prog=prog))
   1801             f = self.__dict__['create_'+frmt]
   1802             f.__doc__ = '''Refer to the docstring accompanying the 'create' method for more information.'''

/usr/local/lib/python3.6/dist-packages/pydot/__init__.py in create(self, prog, format)
   2011 
   2012             if stderr_output:
-> 2013                 stderr_output = ''.join(stderr_output)
   2014 
   2015         #pid, status = os.waitpid(p.pid, 0)

TypeError: sequence item 0: expected str instance, bytes found

for pydotplus 2.0.2 I get:

ModuleNotFoundError                       Traceback (most recent call last)
 in 
      1 G = nx.complete_graph(4)
----> 2 pos = nx.nx_pydot.graphviz_layout(G)
      3 pos = nx.nx_pydot.graphviz_layout(G, prog='dot')

/usr/local/lib/python3.6/dist-packages/networkx/drawing/nx_pydot.py in graphviz_layout(G, prog, root)
    266     This is a wrapper for pydot_layout.
    267     """
--> 268     return pydot_layout(G=G, prog=prog, root=root)
    269 
    270 

/usr/local/lib/python3.6/dist-packages/networkx/drawing/nx_pydot.py in pydot_layout(G, prog, root)
    307 
    308     """
--> 309     import pydot
    310     P = to_pydot(G)
    311     if root is not None:

ModuleNotFoundError: No module named 'pydot'

for pydot I get a parsing error and for pydot2 I get a file not found error.

MajinBoo
  • 307
  • 1
  • 2
  • 10
  • Are you running in a virtual environment? – Paul Brodersen May 04 '20 at 10:54
  • No, I am not doing that. – MajinBoo May 07 '20 at 16:11
  • This is an old question, so I'm not sure if you're still interested in a solution or want to help answering it. But: your code works fine on my system (Windows, Python 3.9.6 64-bit, networkx==2.6.3, pydot==1.4.2). Maybe there was a bug in an older version that has now been fixed. Also: did you have installed `pydot` and `pydot2` at the same time? They seem to be conflicting packages (both use `import pydot`, so they cannot be distinguished from the Python code). – wovano Sep 16 '21 at 19:37

1 Answers1

0

try to set the graph name (e.g. G.name='test')

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
  • 4
    Care to explain what this does and how it solves the problem? – wovano Sep 16 '21 at 19:33
  • 2
    This answer was flagged as [Low Quality](https://stackoverflow.com/help/review-low-quality) and could benefit from an explanation. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers** and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / better than existing answers (if there are any). [From Review](https://stackoverflow.com/review/low-quality-posts/29841486) – Trenton McKinney Sep 18 '21 at 19:16