In this article, we will see how to solve ModuleNotFoundError: No module named matplotlib error if you are also getting the same error. Last night when I was working on a python program, I noticed this error while trying to run the program. So I thought to write an article about this before solving this error so that it will help you folks as well in case you are also facing the same error. While this error could happen due to any reason but most of the time it is due to missing Python matplotlib module in the System.
Just to let you know here I am using Python 3 on Ubuntu 20.04 LTS System. This could be different for you. You might be using Python 2 instead of Python 3. We will see how to solve ModuleNotFoundError: No module named matplotlib error in both the version.
Solved "ModuleNotFoundError: No module named matplotlib"
Also Read: Python: Introduction to Pipenv and Poetry with Best Examples
When I was trying to run a python program which was using subplot()
function from matplotlib module then I noticed ModuleNotFoundError: No module named matplotlib
error as shown below.
NOTE:
root
user to run all the below commands. You can use any user with sudo
access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.root@cyberithub:~# python3 example.py Traceback (most recent call last): File "example.py", line 1, in <module> import matplotlib.pyplot as plt ModuleNotFoundError: No module named 'matplotlib'
So to solve the above error, we need to install the matplotlib using pip3 install matplotlib
command as shown below.
root@cyberithub:~# pip3 install matplotlib Collecting matplotlib Downloading matplotlib-3.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.3 MB) |████████████████████████████████| 11.3 MB 8.1 MB/s Collecting kiwisolver>=1.0.1 Downloading kiwisolver-1.4.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.2 MB) |████████████████████████████████| 1.2 MB 16.4 MB/s Collecting packaging>=20.0 Downloading packaging-21.3-py3-none-any.whl (40 kB) |████████████████████████████████| 40 kB 4.9 MB/s Collecting fonttools>=4.22.0 Downloading fonttools-4.33.3-py3-none-any.whl (930 kB) |████████████████████████████████| 930 kB 6.2 MB/s Collecting cycler>=0.10 Downloading cycler-0.11.0-py3-none-any.whl (6.4 kB) Requirement already satisfied: python-dateutil>=2.7 in /usr/lib/python3/dist-packages (from matplotlib) (2.7.3) Collecting numpy>=1.17 Downloading numpy-1.22.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.8 MB) |████████████████████████████████| 16.8 MB 8.2 MB/s Collecting pyparsing>=2.2.1 Downloading pyparsing-3.0.8-py3-none-any.whl (98 kB) |████████████████████████████████| 98 kB 4.6 MB/s Requirement already satisfied: pillow>=6.2.0 in /usr/lib/python3/dist-packages (from matplotlib) (7.0.0) Installing collected packages: kiwisolver, pyparsing, packaging, fonttools, cycler, numpy, matplotlib Successfully installed cycler-0.11.0 fonttools-4.33.3 kiwisolver-1.4.2 matplotlib-3.5.1 numpy-1.22.3 packaging-21.3 pyparsing-3.0.8
Alternatively, we can also use python3 -m pip install matplotlib --user
command to install the module as shown below.
root@cyberithub:~# python3 -m pip install matplotlib --user Collecting matplotlib Using cached matplotlib-3.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.3 MB) Requirement already satisfied: pillow>=6.2.0 in /usr/lib/python3/dist-packages (from matplotlib) (7.0.0) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.8/dist-packages (from matplotlib) (0.11.0) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.8/dist-packages (from matplotlib) (21.3) Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.8/dist-packages (from matplotlib) (1.22.3) Requirement already satisfied: pyparsing>=2.2.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib) (3.0.8) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.8/dist-packages (from matplotlib) (4.33.3) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib) (1.4.2) Requirement already satisfied: python-dateutil>=2.7 in /usr/lib/python3/dist-packages (from matplotlib) (2.7.3) Installing collected packages: matplotlib Successfully installed matplotlib-3.5.1
If you are using Anaconda3, then you can install using conda install -c conda-forge matplotlib
command as shown below. Check more about this on official documentation.
root@cyberithub:~# conda install -c conda-forge matplotlib
If you are using Python 2, then you just need to run apt-get install python-matplotlib
command as shown below.
root@cyberithub:~# apt-get install python-matplotlib
Or, if you have pip installed then you can also use pip install matplotlib
command as shown below.
root@cyberithub:~# pip install matplotlib
Once the module is installed successfully, I tried running the program again and now I can see that it is working as expected.
Hopefully, this will help you as well in solving "ModuleNotFoundError: No module named matplotlib"
error. Please let me know your feedback on the comment box !!