#15 ModuleNotFoundError: No module named 'graphviz'

Solution 1. for "Ubuntu 20.04.4 LTS”:

sudo apt-get install graphviz

np@np-INTEL:~$ python3
Python 3.8.13 (default, Mar 28 2022, 11:38:47) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import k2
>>> s = '''
... 0 1 -1 0.1
... 1
... '''
>>> fsa = k2.Fsa.from_str(s)
>>> fsa.draw('hello.svg', title='hello')
<graphviz.graphs.Digraph object at 0x7f4cc026ec10>
>>> exit()

Success! output hello.svg below

image

Solution 1. for "Ubuntu 20.04.4 LTS”

sudo apt-get install graphviz

Solution 2: Try pip install

python -m pip install graphviz

Solution 3: Try conda install

Can take a very long time.

conda install -c anaconda graphviz

Output during installation:

np@np-INTEL:~$ sudo apt-get install graphviz
[sudo] password for np: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  g++-8 javascript-common libaccinj64-10.1 libatomic1:i386 libbsd0:i386 libcublas10 libcublaslt10 libcudart10.1 libcufft10 libcufftw10 libcuinj64-10.1 libcupti-dev
  libcupti-doc libcupti10.1 libcurand10 libcusolver10 libcusolvermg10 libcusparse10 libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386 libdrm-radeon1:i386
  libdrm2:i386 libedit2:i386 libegl-dev libegl-mesa0:i386 libegl1:i386 libelf1:i386 libexpat1:i386 libffi7:i386 libgbm1:i386 libgl-dev libgl1:i386 libgl1-mesa-dev
  libgl1-mesa-dri:i386 libglapi-mesa:i386 libgles-dev libgles1 libgles2:i386 libglvnd-dev libglvnd0:i386 libglx-dev libglx-mesa0:i386 libglx0:i386 libjs-jquery
  libjs-underscore libllvm12:i386 libncurses5 libnppc10 libnppial10 libnppicc10 libnppicom10 libnppidei10 libnppif10 libnppig10 libnppim10 libnppist10 libnppisu10
  libnppitc10 libnpps10 libnvblas10 libnvgraph10 libnvidia-cfg1-510 libnvidia-common-470 libnvidia-common-510 libnvidia-compute-510:i386 libnvidia-decode-510
  libnvidia-decode-510:i386 libnvidia-encode-510 libnvidia-encode-510:i386 libnvidia-extra-510 libnvidia-fbc1-510 libnvidia-fbc1-510:i386 libnvidia-gl-510
  libnvidia-gl-510:i386 libnvidia-ml-dev libnvjpeg10 libnvrtc10.1 libnvtoolsext1 libnvvm3 libopengl-dev libopengl0:i386 libpciaccess0:i386 libpthread-stubs0-dev
  libsensors5:i386 libstdc++-8-dev libstdc++6:i386 libthrust-dev libvdpau-dev libvulkan1:i386 libwayland-client0:i386 libwayland-server0:i386 libx11-6:i386 libx11-dev
  libx11-xcb1:i386 libxau-dev libxau6:i386 libxcb-dri2-0:i386 libxcb-dri3-0:i386 libxcb-glx0:i386 libxcb-present0:i386 libxcb-randr0:i386 libxcb-shm0:i386
  libxcb-sync1:i386 libxcb-xfixes0:i386 libxcb1:i386 libxcb1-dev libxdmcp-dev libxdmcp6:i386 libxext6:i386 libxfixes3:i386 libxshmfence1:i386 libxxf86vm1:i386
  mesa-vulkan-drivers:i386 node-html5shiv nsight-compute nsight-systems nvidia-compute-utils-510 nvidia-cuda-dev nvidia-cuda-doc nvidia-cuda-gdb nvidia-dkms-510
  nvidia-kernel-common-510 nvidia-opencl-dev nvidia-prime nvidia-profiler nvidia-settings nvidia-visual-profiler ocl-icd-opencl-dev opencl-c-headers
  screen-resolution-extra x11proto-core-dev x11proto-dev xorg-sgml-doctools xserver-xorg-video-nvidia-510 xtrans-dev
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libann0 libcdt5 libcgraph6 libgts-0.7-5 libgts-bin libgvc6 libgvpr2 liblab-gamut1 libpathplan4
Suggested packages:
  gsfonts graphviz-doc
The following NEW packages will be installed:
  graphviz libann0 libcdt5 libcgraph6 libgts-0.7-5 libgts-bin libgvc6 libgvpr2 liblab-gamut1 libpathplan4
0 upgraded, 10 newly installed, 0 to remove and 64 not upgraded.
Need to get 1,880 kB of archives.
After this operation, 9,205 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
0% [Connecting to banjo.canonical.com (91.189.91.38)]
0% [Connecting to banjo.canonical.com (91.189.91.38)]
Get:1 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libann0 amd64 1.1.2+doc-7build1 [26.0 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libcdt5 amd64 2.42.2-3build2 [18.7 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libcgraph6 amd64 2.42.2-3build2 [41.3 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libgts-0.7-5 amd64 0.7.6+darcs121130-4 [150 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libpathplan4 amd64 2.42.2-3build2 [21.9 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libgvc6 amd64 2.42.2-3build2 [647 kB]
Get:7 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libgvpr2 amd64 2.42.2-3build2 [167 kB]
Get:8 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 liblab-gamut1 amd64 2.42.2-3build2 [177 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 graphviz amd64 2.42.2-3build2 [590 kB]
Get:10 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 libgts-bin amd64 0.7.6+darcs121130-4 [41.3 kB]
Fetched 1,880 kB in 4s (475 kB/s)     
Selecting previously unselected package libann0.
(Reading database ... 280796 files and directories currently installed.)
Preparing to unpack .../0-libann0_1.1.2+doc-7build1_amd64.deb ...
Unpacking libann0 (1.1.2+doc-7build1) ...
Selecting previously unselected package libcdt5:amd64.
Preparing to unpack .../1-libcdt5_2.42.2-3build2_amd64.deb ...
Unpacking libcdt5:amd64 (2.42.2-3build2) ...
Selecting previously unselected package libcgraph6:amd64.
Preparing to unpack .../2-libcgraph6_2.42.2-3build2_amd64.deb ...
Unpacking libcgraph6:amd64 (2.42.2-3build2) ...
Selecting previously unselected package libgts-0.7-5:amd64.
Preparing to unpack .../3-libgts-0.7-5_0.7.6+darcs121130-4_amd64.deb ...
Unpacking libgts-0.7-5:amd64 (0.7.6+darcs121130-4) ...
Selecting previously unselected package libpathplan4:amd64.
Preparing to unpack .../4-libpathplan4_2.42.2-3build2_amd64.deb ...
Unpacking libpathplan4:amd64 (2.42.2-3build2) ...
Selecting previously unselected package libgvc6.
Preparing to unpack .../5-libgvc6_2.42.2-3build2_amd64.deb ...
Unpacking libgvc6 (2.42.2-3build2) ...
Selecting previously unselected package libgvpr2:amd64.
Preparing to unpack .../6-libgvpr2_2.42.2-3build2_amd64.deb ...
Unpacking libgvpr2:amd64 (2.42.2-3build2) ...
Selecting previously unselected package liblab-gamut1:amd64.
Preparing to unpack .../7-liblab-gamut1_2.42.2-3build2_amd64.deb ...
Unpacking liblab-gamut1:amd64 (2.42.2-3build2) ...
Selecting previously unselected package graphviz.
Preparing to unpack .../8-graphviz_2.42.2-3build2_amd64.deb ...
Unpacking graphviz (2.42.2-3build2) ...
Selecting previously unselected package libgts-bin.
Preparing to unpack .../9-libgts-bin_0.7.6+darcs121130-4_amd64.deb ...
Unpacking libgts-bin (0.7.6+darcs121130-4) ...
Setting up liblab-gamut1:amd64 (2.42.2-3build2) ...
Setting up libgts-0.7-5:amd64 (0.7.6+darcs121130-4) ...
Setting up libpathplan4:amd64 (2.42.2-3build2) ...
Setting up libann0 (1.1.2+doc-7build1) ...
Setting up libcdt5:amd64 (2.42.2-3build2) ...
Setting up libcgraph6:amd64 (2.42.2-3build2) ...
Setting up libgts-bin (0.7.6+darcs121130-4) ...
Setting up libgvc6 (2.42.2-3build2) ...
Setting up libgvpr2:amd64 (2.42.2-3build2) ...
Setting up graphviz (2.42.2-3build2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.7) ...
np@np-INTEL:~$ which dot
/usr/bin/dot

Original Problem was:

After successfully downloading k2 https://k2-fsa.github.io/k2/installation/conda.html, I tried to use the graphviz package. Got ModuleNotFoundError: No module named 'graphviz'

np@np-INTEL:~$ python3
Python 3.8.13 (default, Mar 28 2022, 11:38:47) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import k2
>>> s = '''
...  0 1 -1 0.1
... 1
... '''
>>> fsa = k2.Fsa.from_str(s)
>>> fsa.draw('hello.svg', title='hello')
You cannot use `to_dot` unless the graphviz package is installed.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/np/anaconda3/lib/python3.8/site-packages/k2/fsa.py", line 352, in draw
    digraph = k2.utils.to_dot(self, title=title)
  File "/home/np/anaconda3/lib/python3.8/site-packages/k2/utils.py", line 126, in to_dot
    import graphviz
ModuleNotFoundError: No module named 'graphviz'
>>> exit()
np@np-INTEL:~$ 

Attempts that didn’t work

conda install -c anaconda graphviz

partial output that shows that it was installed but actually: “ModuleNotFoundError: No module named 'graphviz'”

- yaml -> libgcc-ng[version='>=7.3.0'] -> __glibc[version='>=2.17']
  - yarl -> libgcc-ng[version='>=7.5.0'] -> __glibc[version='>=2.17']
  - zeromq -> libgcc-ng[version='>=7.3.0'] -> __glibc[version='>=2.17']
  - zfp -> libgcc-ng[version='>=7.5.0'] -> __glibc[version='>=2.17']
  - zlib -> libgcc-ng[version='>=7.5.0'] -> __glibc[version='>=2.17']
  - zope.interface -> libgcc-ng[version='>=7.5.0'] -> __glibc[version='>=2.17']
  - zstd -> libgcc-ng[version='>=7.5.0'] -> __glibc[version='>=2.17']

Your installed version is: 2.31


np@np-INTEL:~$ 
np@np-INTEL:~$ python3
Python 3.8.13 (default, Mar 28 2022, 11:38:47) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import k2
>>> s = '''
... 0 1 -1 0.1
... 1
... '''
>>> fsa = k2.Fsa.from_str(s)
>>> fsa.draw('hello.svg', title='hello')
You cannot use `to_dot` unless the graphviz package is installed.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/np/anaconda3/lib/python3.8/site-packages/k2/fsa.py", line 352, in draw
    digraph = k2.utils.to_dot(self, title=title)
  File "/home/np/anaconda3/lib/python3.8/site-packages/k2/utils.py", line 126, in to_dot
    import graphviz
ModuleNotFoundError: No module named 'graphviz'
>>> exit()
np@np-INTEL:~$ python

also didn’t work

np@np-INTEL:~$ conda install graphviz=2.50.0 -c pkgs/main

pip installation also didn’t work, couldn’t locate dot package

np@np-INTEL:~$ python -m pip install graphviz
Collecting graphviz
  Downloading graphviz-0.20-py3-none-any.whl (46 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 47.0/47.0 kB 1.1 MB/s eta 0:00:00
Installing collected packages: graphviz
Successfully installed graphviz-0.20
np@np-INTEL:~$ python3
Python 3.8.13 (default, Mar 28 2022, 11:38:47) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import k2
>>>  s = '''
  File "<stdin>", line 1
    s = '''
    ^
IndentationError: unexpected indent
>>> s = '''
... 0 1 -1 0.1
... 1
... '''
>>> fsa = k2.Fsa.from_str(s)
>>> fsa.draw('hello.svg', title='hello')
Traceback (most recent call last):
  File "/home/np/anaconda3/lib/python3.8/site-packages/graphviz/backend/execute.py", line 81, in run_check
    proc = subprocess.run(cmd, **kwargs)
  File "/home/np/anaconda3/lib/python3.8/subprocess.py", line 493, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/home/np/anaconda3/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/home/np/anaconda3/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: PosixPath('dot')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/np/anaconda3/lib/python3.8/site-packages/k2/fsa.py", line 363, in draw
    temp_fn = digraph.render(filename='temp',
  File "/home/np/anaconda3/lib/python3.8/site-packages/graphviz/_tools.py", line 171, in wrapper
    return func(*args, **kwargs)
  File "/home/np/anaconda3/lib/python3.8/site-packages/graphviz/rendering.py", line 122, in render
    rendered = self._render(*args, **kwargs)
  File "/home/np/anaconda3/lib/python3.8/site-packages/graphviz/_tools.py", line 171, in wrapper
    return func(*args, **kwargs)
  File "/home/np/anaconda3/lib/python3.8/site-packages/graphviz/backend/rendering.py", line 324, in render
    execute.run_check(cmd,
  File "/home/np/anaconda3/lib/python3.8/site-packages/graphviz/backend/execute.py", line 84, in run_check
    raise ExecutableNotFound(cmd) from e
graphviz.backend.execute.ExecutableNotFound: failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH
>>> 
>>>