How to Solve ImportError: No module named error in Python

To solve ImportError: No module named error in Python; check the typographical error or check if the module is installed on the system or not. No module named error Is a common error raised when the imported module is not installed in the system.

The ImportError can occur for several reasons, like the typed module name may contain some typographical errors. Or the module is not installed in the system.

Importing the module is an important task to use that module. Without importing a module, the methods inside those modules cannot be used for processing purposes.

Solve ImportError by checking Typographical Error

Check whether the typed module name is correct. If any spelling mistakes are there, that may be the reason for this error. Let us see an example of this:

import npy
arr = npy.array([5,6,7,8,9])
print(arr)

In this example, we used a numpy package, but due to typographical error, the typed module name is npy. So even though the array creation is correct, the array will not be created. Furthermore, because the array method is inside numpy, this numpy is itself not imported correctly. So the array cannot be made.

If you run this program, there will be an error saying no module named npy.

Output

Traceback (most recent call last):
File "/Users/krunallathiya/Desktop/Code/R/app.py", line 1, in <module>
import npy
ModuleNotFoundError: No module named 'npy'print(arr)

To solve this problem, correct the spelling mistakes.

import numpy
arr = numpy.array([5,6,7,8,9])
print(arr)

Output

[5 6 7 8 9]

Solve ImportError by checking if a module is installed

Many built-in modules come along with the Python installation itself. But many other modules are not installed in the system. To use those modules, we have first to install that module. So if we import that module without installing it, then the ImportError error named ModuleNotFound error is raised.

import numpy
arr = numpy.array([5,6,7,8,9])
print(arr)

Numpy is not a built-in package. Therefore, the above program will create an error named ModuleNotFoundError. If the numpy package is not installed before importing, this error will be raised.

To check whether a package is installed or not, we can use a try-except block. By using this try-except block, we need to know whether the package is installed already or not.

try:
  import numpy
  arr = numpy.array([5,6,7,8,9])
  print(arr)
except ModuleNotFound:
  print("The package is not installed in the system")

Output

[5 6 7 8 9]

To install numpy, we can use the pip command. Open the terminal and type:

python3 -m pip install numpy 

This command is used to install the numpy package. We can use this pip command to install all the modules.

That’s how you can avoid the ImportModuleError or if it occurred then you need to install that module.

That’s it for this tutorial.

Related posts

How to Import from Parent Directory in Python

How to Import Files in Python

How to Throw Error in Python

Leave a Comment