4

I am trying to use the following code segment to save a tree image generated from fitting a random forest model

# Import tools needed for visualization
from sklearn.tree import export_graphviz
import pydot
# Pull out one tree from the forest
tree = rf.estimators_[5]
# Export the image to a dot file
export_graphviz(tree, out_file = 'tree.dot', feature_names = feature_list, rounded = True, precision = 1)
# Use dot file to create a graph
(graph, ) = pydot.graph_from_dot_file('tree.dot')
graph.write_png('tree.png')

But the graph.write_png('tree.png') generates the following error message, what might be the reason?

FileNotFoundError                         Traceback (most recent call last)
~\Anaconda3\envs\dsproject\lib\site-packages\pydot.py in create(self, prog, format, encoding)
   1860                 shell=False,
-> 1861                 stderr=subprocess.PIPE, stdout=subprocess.PIPE)
   1862         except OSError as e:

~\Anaconda3\envs\dsproject\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    755                                 errread, errwrite,
--> 756                                 restore_signals, start_new_session)
    757         except:

~\Anaconda3\envs\dsproject\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1154                                          os.fspath(cwd) if cwd is not None else None,
-> 1155                                          startupinfo)
   1156             finally:

FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-15-a132c22204b9> in <module>()
----> 1 graph.write_png('tree.png')

~\Anaconda3\envs\dsproject\lib\site-packages\pydot.py in new_method(path, f, prog, encoding)
   1671                 self.write(
   1672                     path, format=f, prog=prog,
-> 1673                     encoding=encoding)
   1674             name = 'write_{fmt}'.format(fmt=frmt)
   1675             self.__setattr__(name, new_method)

~\Anaconda3\envs\dsproject\lib\site-packages\pydot.py in write(self, path, prog, format, encoding)
   1754                 f.write(s)
   1755         else:
-> 1756             s = self.create(prog, format, encoding=encoding)
   1757             with io.open(path, mode='wb') as f:
   1758                 f.write(s)

~\Anaconda3\envs\dsproject\lib\site-packages\pydot.py in create(self, prog, format, encoding)
   1861                 stderr=subprocess.PIPE, stdout=subprocess.PIPE)
   1862         except OSError as e:
-> 1863             if e.errno == os.errno.ENOENT:
   1864                 args = list(e.args)
   1865                 args[1] = '"{prog}" not found in path.'.format(

AttributeError: module 'os' has no attribute 'errno'
user288609
  • 12,465
  • 26
  • 85
  • 127

1 Answers1

3

In python 3.7 os.errno is no longer available, you can modify python3.7/site-packages/pydot.py to use import errno directly and line 1863 change to:

import errno
if e.errno == errno.ENOENT:
Ehsan
  • 3,711
  • 27
  • 30
Ditiz
  • 171
  • 2
  • 12