Fix: importerror: no module named ‘matplotlib’

The “matplotlib” is the python library for creating interactive visualization through plots. While dealing with the “matplotlib” library, an error “importerror: no module named ‘matplotlib’” comes across for several reasons.

This guide will provide multiple solutions to encounter the above-stated error with practical implementation: The supported content of this article is as follows:

Let’s get started.

Reason: Not Installed “matplotlib” Library

One of the most prominent reasons for this error is that the “matplotlib” library is not installed in the system. During the execution of the “test.py” python file, the error comes across in the Ubuntu terminal, as seen below:

In the next section, we will discuss its solution to resolve this error.

Solution 1: Install “matplotlib” Library

The easiest solution is to install the “matplotlib” library to encounter the error mentioned above. After installing the module in the operating system, the python file will execute successfully. To do so, the installation command for several Linux distributions is provided below:

For Ubuntu/Debian:

$ sudo apt install python3-matplotlib

For Fedora:

$ sudo dnf install python3-matplotlib

For Arch Linux:

$ sudo pacman -S python-matplotlib

For RedHat:

$ sudo yum install python3-matplotlib

After installation of the module, the user can execute the specified python file on which a specific error is generated. For instance, the below script will execute the “test.py” file from the terminal:

$ python3 test.py

You can verify that the “matplotlib” library has been successfully installed in the operating system.

Solution 2: Install the “matplotlib” Library Using the pip Package Manager

To resolve the error, an alternative solution is provided to install the “matplotlib” library from the pip package manager. The step-by-step procedure is given below for installing the “matplotlib” library:

Install pip Package Manager

To install the package manager “pip” in Linux, execute one of the below appropriate scripts as per your Linux distro:

For Debian based:

$ sudo apt install python3-pip

And for RedHat/CentOS/Fedora, use the command provided below:

$ sudo yum install python3-pip

Install “matplotlib” Library

After the installation of the package manager, execute the below script for importing the “matplotlib” library from the “pip” package manager:

$ sudo pip3 install matplotlib

The output shows that the “matplotlib” library has already been installed in the operating system.

Verify Imported “matplotlib” Library

To verify the “matplotlib” library, execute the “test.py” python file on which the error is generated. The below script will execute the particular file:

$ python3 test.py

The output confirms that the “test.py” file has been successfully executed without any error.

Conclusion

In Linux, the error “importerror: no module named ‘matplotlib’” is generated when the library ‘matplotlib’ is not installed in the operating system. To fix the error, install the “matplotlib” module in your respective Linux distribution. This guide has provided possible solutions to resolve the error along with the installation of ‘matplotlib’ in other Linux-based distributions.