Fix: No Module Found Error in python

Sometimes while importing a module we got a famous error “No Module Found” or sometimes “IndexError: No Module named”, this is a very common error and quite frustrating at the same time, we reinstall the model again and again but every time when we try to import the model we got the same error. So let’s learn why this error come and how to remove this one for all

First of all, let’s import a Module and see if it working fine.

command:

 import matplotlib 

Here note that I am using python 3.7 and the module is imported successfully because it is installed on the correct path.

Now let’s delete the module and then we will try import the model again to see what happens.

For mac user the file path is Library>Framework>python.framework>versions>3.7>lib?python3.7>site-packages

Site-packages is the folder in which all the module for that python is stored.

Here lets delete these 2 files and let’s try to import the module once again

command:

 import matplotlib 

As you can we are getting the error “ModuleNotFoundError: No module named ‘matplotlib’”.

So one thing is clear, this error comes because the module is not present in the site-packages folder of the respective version of python. No matter how many times we installed the module if the error came then the module is not available in this folder.

But you may argue that you installed the module dozens of time using pip and getting the same error. So let’s install the module

command:

 pip install matplotlib 

Here you can see the requirement is already satisfied in version 2.7,

This is because, by default, the python 2.7 is installed in our system(in mac), One more thing I want to share with you is

pip command can also be written as
python -m pip install matplotlib

to prove the point lets type the command and see what happens

command:

 python -m pip install matplotlib 

and you can see we are getting the same error as above.

So we can conclude that the ‘python’ in the command is the version of python in which pip is installing the module. For python version 2.x we use python, for python 3.x we use python3

let’s change it to python3

command:

 python3 -m pip install matplotlib 

Here we are getting the same error that requirement already satisfied but this time the version is different, here the version is 3.6 but we are trying to import the module in version 3.7.

First of all let’s check which path is set, just type

command:

 echo $PATH 

Echo is a command which calls back whatever you type after it, try echo python. anyways,

Here you can see that the path is set to 3.6 and this is the reason we are not able to install the module in 3.7

This means that we can import the module in Python 2.7 as well as Python 3.6 without any error, so for better understanding lets run the command and see what happens.

We can see the module has imported successfully in python 2.7 and 3.6.

So we can conclude that we can access version 2.7 via python and version 3.6 via python3 but how can we access version 3.7 so that we can install the module straight to python 3.7 ?

For this, we have to change the path so that we can access python 3.7 using python3 and to do so we need our tiny little nano editor, Just type this command in Terminal or Command Prompt

command:

 nano .bash_profile

Here you can see there are the various path which belongs to a different version of python,

These paths usually add to this bash profile at the time of installation.

Now what we have to do is to uncomment the version of python which we want to use and comment the rest as shown below, here in our case we need python 3.7 so we uncomment the python 3.7 path and comment any other path

To save the file hit control + X and then press Y for yes, and then Enter to exit the nano editor.

Our path is set to 3.7, Now just restart the terminal and type the echo command to check if the path is set to 3.7 or not.

command:

 echo $PATH 

and yes the path is set to Python 3.7, which means we can now access python 3.7 via python3

So let’s install the module

command:

 python3 -m pip install matplotlib

and as you can see the module is installed successfully. Even though it uses the cached memory because in this case matplotlib is already installed in other versions.

For the final test lets import it to verify,

command:

 import matplotlib 

Here we go, the module import successfully without any sort of error.

In the same way, we can change the version in which we want to install the module. Since TensorFlow is not available for Python 3.7 so I am using python3.6 to work with it.

Thanks for reading, Hope it resolves your problem. You can also comment or mail me if you persist with any problem and I will try my best to resolve it.



1 thought on “Fix: No Module Found Error in python”

Leave a Reply

Your email address will not be published. Required fields are marked *