ModuleNotFoundError: No module named ‘graphviz’

Graphviz is an open-source Python module for visualization. When you try to import the module for visualization purposes without installing it, you will get ModuleNotFoundError: No module named ‘graphviz’ error. The error clearly says that the grahviz module is not found which means it is not installed on your system. The simplest way to get rid of the error is to install the module on your system and then import it for visualizations. In this short article, we will discuss how we can install the Graphviz module on our system using various methods to get rid of the ModuleNotFoundError: No module named ‘graphviz’ error.

ModuleNotFoundError: No module named ‘graphviz’ – Possible Solutions

The error is usually caused by not having a module installed on the system and then importing the module to access its functionalities. We know that the graphviz module is an open-source Python module that is used to visualize various kinds of plots. Especially, it is well known for visualizing tree or graph structures. For example, it can be used to visualize neural networks, decision trees, or random forests.

If you will try to access the functionalities in the graphviz module without installing it on your system, then you will get ModuleNotFoundError: No module named ‘graphviz error.

Using Pip command to install grahviz

One of the best and simplest methods in Python to install the modules is to use the Pip command in order to install the module on your system. Here we will list possible pip commands that you can use in order to install the module. You can use any of them.

# using pip command
pip install grahviz

#using pip3 command
pip3 install grahviz

# # on linux operating system
sudo pip3 install grahviz

# on windows
pip install grahviz --user

# using pip version
python -m pip install grahviz

#using pip3 version
python3 -m pip install grahviz

Hopefully, the mentioned commands will help you to install the module and you will no longer get the error.

Using a virtual environment

In some cases, you may forget to install the modules in your virtual environment. It is necessary to install all the modules in your virtual environment before using them.

Let us first see how we can activate the virtual environment on our system.

# create virtual environment
python3 -m venv venv

#  activate on Unix or MacOS
source venv/bin/activate

# activate on Windows (cmd.exe)
venv\Scripts\activate.bat

# activate on Windows (PowerShell)
venv\Scripts\Activate.ps1

Once you are inside the virtual environment, you need to install the modules again.

pip install graphviz

Now, you will no longer get the error.

Alternative methods of installation

If none of the above-mentioned methods helped you to install the module on your system, then you can follow these commands.

If you are an Ubuntu user, then I recommend using the given command to install the Graphviz module.

sudo apt-get install -y graphviz libgraphviz-dev

If you are using an anaconda environment, then you need to use the following commands:

conda install -c anaconda python-graphviz

conda install -c anaconda pydot

Hopefully, these methods will help you to solve the error.

Understanding the ModuleNotFoundError: No module named ‘graphviz’

There can be many reasons for getting this error. One of the possible reasons is to use the Graphviz module on your system without installing it on your system. The error clearly says that the module was not found which means it is not yet installed on our system.

Examples of using the Graphviz module

We know that the Garphviz module is used to visualize tree-like structures. We will now take a simple example and see how we can visualize content using the Graphviz module.

# importing the module
import graphviz

# plotting the graph
a = graphviz.Digraph()
a.edge('Name', 'Bashir')

a

Another really cool feature is to visualize the data in a tree structure.

import graphviz  

w = graphviz.Digraph('wide')

w.edges(('0', str(i)) for i in range(1, 10))


w.view()

The good thing is that this graph is in pdf format so you can easily use and send it.

Summary

In this article, we discussed the reasons for getting ModuleNotFoundError: No module named ‘graphviz’ error in Python. We also covered various methods to solve the error. The error usually occurs when the module is not installed on our system.

Related Issues

Leave a Comment