1

I try visualize a Keras Model using Ipython and from keras.utis.vis_utils. But i receive the following error.

ImportError: Failed to import pydot. Please install pydot. For example > with pip install pydot.

Code used :

E.g model = ggv.h5

from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(model).create(prog='dot', format='svg'))

Traceback

Traceback (most recent call last):
  File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\IPython\core\interactiveshell.py", line 2869, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-49-b1aadf48e3ac>", line 5, in <module>
    SVG(model_to_dot(model).create(prog='dot', format='svg'))
  File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\keras\utils\vis_utils.py", line 55, in model_to_dot
    _check_pydot()
  File "C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\lib\site-packages\keras\utils\vis_utils.py", line 20, in _check_pydot
    'Failed to import `pydot`. '
ImportError: Failed to import `pydot`. Please install `pydot`. For example with `pip install pydot`.

I tried to uninstall pydot, graphviz and again i installed it. But the error remains same.

In that case what will be the best solution to visualize the model

Mari
  • 698
  • 1
  • 8
  • 27

1 Answers1

2

If you are getting that error in a jupyter notebook there is a chance you are using a python interpreter different from the system default.

In the notebook try this:

import sys
%$sys.executable -m pip install pydot

This snippet will pass the path of your current python interpreter to the terminal and run pip as a module to install the library.

Check this links for further info

If you are using another interpreter, then first discover which one you are using: print(sys.executable)

Then use that information to run in a terminal: path_to_the_interpeter -m pip install pydot

Another way would be to do it from within your code:

import subprocess
import sys

def install(library):
    subprocess.call([sys.executable, "-m", "pip", "install", library])

try:
    import pydot
except ImportError:
    install('pydot')
    import pydot

But I do prefer installing libraries from the terminal and keep my code clean.

alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • @ponraj.rajesh The concept is the same, first you need to know the path to your interpreter, then you can install it using the `-m` option. Check the updated answer. – alec_djinn Mar 29 '19 at 09:23
  • My path to interpreter after executing `print(sys.executable)` is `C:\Users\VW3ZTWS\PycharmProjects\Data_Collection_and_learnings\venv\Scripts\python.exe`. Now i tried to install it thru' code `import subprocess import sys def install(library): subprocess.call([sys.executable, "-m", "pip", "install", library]) install('pydot')` – Mari Mar 29 '19 at 09:39
  • After executing it shows `Requirement already satisfied: pydot in c:\users\vw3ztws\pycharmprojects\data_collection_and_learnings\venv\lib\site-packages (1.4.1) Requirement already satisfied: pyparsing>=2.1.4 in c:\users\vw3ztws\pycharmprojects\data_collection_and_learnings\venv\lib\site-packages (from pydot) (2.3.0)` **Now i tried to visualize using above mentioned line of codes, but the same error repeats...** @alec_djinn – Mari Mar 29 '19 at 09:44
  • Then I don't know. Try uninstalling it and reinstalling it. – alec_djinn Mar 29 '19 at 09:49
  • No, pydot, or the library requiring it – alec_djinn Mar 29 '19 at 09:59