ModuleNotFoundError No Module Named dtreeviz in Python

When you want to perform some visualization and you forget to install dtreeviz module, then you will get ModuleNotFoundError: No module named dtreeviz error. This error occurs when you try to access the functions in the dtreeviz module without installing it on your system. In this short article, we will discuss various possible reasons to get this error and we will go through possible solutions to solve ModuleNotFoundError No module named dtreeviz error as well.

Solve ModuleNotFoundError: No module named dtreeviz

The error ModuleNotFoundError No module named dtreeviz occurs when you try to use the functionalities of dtreeviz module without installing it on your system. The dtreeviz is a python module that is mostly used to visualize decision trees. A decision tree is a supervised machine-learning algorithm that is used to make predictions. Once the model is trained, we can use the dtreeviz module in order to visualize the decision trees.

We will now go through various possible solutions to solve the error.

Using the pip install command

In Python, it is very easy to install modules. We can use the pip command in order to install some modules on our system. To install the dtreeviz on your system, you can use any of the following commands depending you your system.

# using pip command
pip install dtreeviz

#using pip3 command
pip3 install dtreeviz

# # on linux operating system
sudo pip3 install dtreeviz

# on windows
pip install dtreeviz --user

# using pip version
python -m pip install dtreeviz

#using pip3 version
python3 -m pip install dtreeviz

If you are using a Jupyter Notebook or Anaconda environment, then you can use the following commands to install the dtreeviz on your system.

# for jupyter notebook
!pip install dtreeviz

# for anaconda
conda install dtreeviz

Hopefully, any of the mentioned methods will help you to install the module on your system.

Using the virtual environment

In case you are using a virtual environment, then you also need to install the module on your virtual environment as well.

You can use the following commands in order to create and activate the virtual environment.

# 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 the virtual environment is created and activated, you can then install the dtreeviz module in order to get rid of ModuleNotFoundError: No module named dtreeviz error.

pip install dtreeviz

Understanding the ModuleNotFoundError: No module named dtreeviz error

Similar to any other errors in Python, this error also has two main parts which help us to figure out the reason for getting the error. The first part of the error gives information about the category of the error and in our case, the category of the error is ModuleNotFound which means we are using some functions from the module without actually importing the module.

However, the second part of the error gives more specific information and helps us to figure out which module is missing in our system. It clearly says that No module named ‘dtreeviz’ which means the module that we are going to use or trying to import is not yet installed on our system.

Visualize decision trees using dtreeviz

Let us now use the dtreeviz to visualize a decision tree. In our case, we will not go into the detail of how to train the model and step-by-step visualization process.

Here we will just take an example and see how we can visualize a decision tree using dtreeviz module.

# importing the module
from dtreeviz.trees import dtreeviz

# visualizing the decision tree
viz = dtreeviz(rf.estimators_[0])

# saving the module
viz.save("decision_tree_wine.svg")

Related Issues

Leave a Comment