How to fix Matplotlib modulenotfound import errors?

In today’s post i would like to provide some troubleshooting information for people installing the matplotlib library package in their computers.

ModuleNotFoundError: no module named ‘matplotlib’ pyplot

This error is thrown when Python can’t find the Matplotlib package in your development environment. Most probably you’ll be able to troubleshoot this error by downloading the library from the Python repository using the PIP utility.

Install matplotlib with pip

In order to troubleshoot this error follow this process (explained for Windows, might require minor adjustments for MAC and Linux). Note that the process is identical for most Python add on libraries / packages such as Seaborn, NumPy, Pandas and many others.

  • Save your work, and close your Jupyter Notebooks/Lab, Spyder or other development environment you might be using.
  • Open your Command Prompt.
  • Type cmd.
  • Then in the command prompt type, type the following command:
pip install matplotlib
  • Now, go ahead and hit Enter.
  • Python will download the latest version of the matplotlib library from the Python package repository.
  • Once done, open your development environment and import matplotlib.
  • That’s it!

Anaconda environments

If you are have installed Anaconda, then by default, the basic Data Analysis packages, including Pandas and Matplotlib are already installed in your base environment. If for some reason you still receive an import error proceed as following.

  • From the Start mane, open Anaconda Navigator.
  • On the left hand side, hit the Environments tab (step 1)
  • Then highlight your Anaconda environment (step 2).
  • Then in the dropdown box (step 3), select Not Installed.
  • In the Search box, type matplotlib and hit Enter.
  • Then check the box near the package and hit Apply.
  • Then in the Install Packages dialog, click Apply again.
  • Once Anaconda Navigator is done, close it and reopen your development environment and download matplotlib.

Note: for completeness, you can also update your Anaconda environment manually (this is specially useful if you use MiniConda.

  • From the Windows Start Menu open the Anaconda Prompt
  • Type the following command
conda activate <path_to_your_environment>
  • Hit Enter.
  • Now type
conda install matplotlib
  • Hit Enter.
  • Matplotlib will be downloaded and installed.
  • That’s it.

Importerror no module named matplotlib.pyplot / matplotlib.path

This error is the base class of the ModuleNotFound error, and available since Python 3.3. The troubleshooting process we just described will remedy this error as well.

Questions? Feel free to leave us a comment

Leave a Comment