Проблема с pydot при попытке нарисовать простой график с networkx и graphviz - PullRequest
1 голос
/ 03 мая 2020

Я хотел нарисовать простой график с networkx и graphviz, но, похоже, проблема с pydot.

Я использую

  • Ubuntu 18.04.4 LTS
  • Python 3.6.9
  • graphviz 0.14
  • networkx 2.4
  • новейшие версии pydot, pydot2, pydot3 и pydotplus

Я пытался запустить:

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')

, но, похоже, выдает ошибку, когда независимо от того, какой модуль pydot я использую, и я не знаю почему.

для pydot2 1.0.33 Я получаю:

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

для pydot3 Я получаю:

---------------------------------------------------------------------------
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

для pydotplus 2.0.2 Я получаю:

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'

для pydot Я получаю ошибку синтаксического анализа и для pydot2 получаю ошибку "файл не найден".

...