[Solved] modulenotfounderror: no module named 'matplotlib'

[Solved] modulenotfounderror: no module named 'matplotlib'

The Python "ModuleNotFoundError: No module named 'matplotlib'" error appears when:

  • Not having the matplotlib module installed
  • Installing and using different Python versions
  • Using python <= 2
  • Creating a virtual environment with Python 2
  • Installing matplotlib globally

In this article, we'll see the possible solutions to solve this error. If you are ready, let's get started.

Solution 1: Installing the matplotlib module

About 90% of the "ModuleNotFoundError: No module named 'matplotlib'" error is because the matplotlib module is not installed. However, To solve the problem, we need to install the module.

Choose your preferred way to install matplotlib (PIP is recommended)

Pip:

# 👇️ Python 2
pip install matplotlib

PIp 3:

# 👇️ Python 3
pip3 install matplotlib

If you get something like a permission error, Run the following command:

sudo pip3 install matplotlib

Anaconda:

conda install -c conda-forge matplotlib

After installing the module packages, execute your code again. If the error has been fixed, congratulation. If not, then move to the next solution.

Solution 2: Ensure Using Python > 2

As you know, most python libraries don't support Python 2 anymore. And probably you use a new version of matplotlib that dropped using Python 2.

At this current date, Matplotlib 2.0. x supports Python versions 2.7 through 3.10. However, we recommended using Python 3.

If Python 3 is not installed on your device, Let's install it.

Installing Python 3 on Linux

If you are in Ubuntu 16.10 or newer, You can run these commands to install Python 3.

$ sudo apt-get update
$ sudo apt-get install python3.6

If you’re using another version of Ubuntu:

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6

If you are using other Linux distributions:

sudo dnf install python3

Installing Python 3 on Windows:

To install Python 3 on Windows, follow these steps.

  1. Go to the Downloads for Windows section of the official Python website on your browser.
  2. Search for your desired version of Python. Python 3 release is version Python 3.10.7.
  3. Click a link to download the Windows x86-64 (64bit) or Windows x86 (32bit) .
  4. Run the program and follow the steps to install it.

Installing Python 3 on Mac:

Before installing Python 3, you must install GCC . GCC by downloading Xcode , the smaller Command Line Tools . You also need to install brew.

Here is the brew installation command:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Add the following line at the end of your ~/.profile file.

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

Use this If you have OS X 10.12 (Sierra) or older :

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

Now, let's install Python 3:

brew install python

After installing Python 3:

  1. Create a new virtual env using Python 3
  2. Install matplotlib using PIP3
  3. Execute your code

If you don't have virtualenv, You will find out how to install it in the next solution.

Solution 3:Instaling matplotlib In virtualenv

Another reason for "ModuleNotFoundError: No module named 'matplotlib'" is you install the matplotlib package globally without a Virtual Environment . However, Let's see how to set up a virtualenv with Python 3

Linux

Update all Linux packages:

root@Py:~# apt-get update -y

Install python3-venv:

root@py:~# apt-get install -y python3-venv

Install virtualenv using pip:

pip3 install virtualenv

Create  Virtual Environment:

root@py:~# python3 -m venv py_venv

py_venv is the name of my virtual envormment. Now let's activate the Virtual Environment:

root@py:~# source py_venv/bin/activate

Congratulations, Our Virtual Environment is ready to install the module packages.

Windows

Install virtualenv using pip:

pip3 install virtualenv

Create the virtual environment:

cd my-project
virtualenv --python C:\Path\To\Python\python.exe py_env

Activate it:

.\py_env\Scripts\activate

Done!

Mac

Install virtualenv using pip

sudo pip install virtualenv

If for any error, run the following command:

sudo -H pip install virtualenv

Create py_env virtual environment :

virtualenv py_env

Activate virtualenv:

source py_env/bin/activate

After activating the virtual environment, run the following command (pip3) to install matplotlib:

pip3 install matplotlib

This command works with all systems (Linux, Windows, Mac)

Solution 4: Installing and executing with the same Python version

You probably got the error because you have installed the module and executed your code with different versions of python. For example:

We use pip ( Python 2 ) to install the matplotlib module and run the code with Python 3 .

# 👇️ Python 2
pip install matplotlib

And execute our code with Python 3:

# 👇️ Execute The code with Python 3
python3 my_code.py

So, If you install with pip , execute using Python 2 otherwise, if you install with pip3 , execute Python 3 .

Conclusion

In this article, we've learned about 4 solutions to solve "ModuleNotFoundError: No module named 'matplotlib'" . I Hope You have solved the error. For more info about ModuleNotFoundError , visit modulenotfounderror no module named in python .

Happy Codding </>