Summary: The most common cause of ModuleNotFoundError
is a faulty installation of the module or importing a module incorrectly. For a successful installation of the module, use pip3 install numpy
.
Table of Contents
◈ Overview
If you are someone like me who works with lots and lots of data, then Import Error: No module named 'xyz'
is probably one of the most common errors that you have come across. In this article, I will discuss the causes and solutions to this error in Python.
Example: Suppose that you are trying to import the Numpy
library and print an array. However you get an ImportError: ModuleNotFoundError: No module named 'numpy'
.
1 2 3 4 5 |
import numpy as np arr = np.array((1, 2, 3, 4, 5)) print(arr) |
Output:
➥ Reasons of Import Error: No module Named ‘xyz’
Two major reasons that lead to the occurrence of this error:
- You have not installed the module correctly, i.e., you are using an older version of the module, which is not compatible with the version of Python you are using.
- The Python-version/environment where you installed the package is not the same.
- Note: This usually happens when you have two versions of Python (Python 2.x and Python 3.x simultaneously) installed on your system.
◈ Solution
Let us dive into the probable solutions to our problem.
❋ Method 1: Fixing Faulty Installation
If you are on Python version 3, then you must install the latest version of the package. In our case, we must install the latest version of Numpy
. Before we dive into the commands to install the latest version of Numpy
, let us look at the following visual, which explains what happens when you try to install an older version of Numpy
on Python 3.
Thus, it is clear from the above example that even though you installed Numpy
, the ModuleNotFoundError was not resolved. The reason is you installed an older version of Numpy
,which is incompatible with Python 3.
Note:
- The command to install a particular version of
Numpy
is:-pip install numpy==x.y.z
- Here x.y.z re[resents the version of
Numpy
you want to install, for example:-numpy==1.8.2
- Here x.y.z re[resents the version of
To resolve this issue, you can use the following command in your terminal to ensure that the latest version of Numpy
is installed:
pip3 install numpy
Let’s have a look at the following graphic to visualize the solution.
❋ Method 2: Fixing Missing Path
Sometimes, even the above procedure does not work. Whenever you import a module, python searches the module in specific directories.
To get hold of all the directories that Python will search, you can use piece of code:
1 2 3 4 5 |
import sys for path in sys.path: print(path) |
Output:
Disclaimer: The paths shown in this output will vary from user to user.
You should make sure that the NumPy
module resides in any of these directories. Once a module has been imported, you can find its location with the help of the module’s __file__
attribute:
1 2 3 4 |
import numpy as np print(np.__file__) |
Output:
Disclaimer: The paths shown in this output will vary from user to user.
C:\Users\DELL\AppData\Roaming\Python\Python38\site-packages\numpy\__init__.py
If the NumPy
module is not found in any of the listed directories, then you have to append it to the python search path using the following statements:
1 2 3 4 5 |
import sys sys.path.append("Path to NumPy Module") import numpy as np |
You also have other options to ensure that your module is found. These are:
- Put
module.py
inside the directory containing the input script. - Modify the environment variable: PYTHONPATH and ensure that it contains the directory where
module.py
is located before you start the interpreter.- You can also opt to put
mod.py
in one of the directories already present in thePYTHONPATH
environment variable.
- You can also opt to put
In case you are struggling with this error despite following all the above methods, and you are using Jupyter Notebook, then you might want to have a look at this article.
◈ Scenario 2: ModuleNotFoundError In Case Of User-Defined Modules
Previously we found out how to resolve the ModuleNotFoundError
when we are dealing with in-built modules/packages. If you are working with user-defined modules, you may still encounter this problem.
Example: Consider that you have created a user- defined module ex
. This module is within a directory named UserDefinedModule
.
1 2 3 4 |
def foo(): print("This is a User Defined Module!") |
Now you want to import this module into your program as shown below:
1 2 3 4 5 |
import ex print('Python!') ex.foo() |
Output:
Explanation:
The module ex
is not a part of the current working directory. Therefore, Python is unable to import the required module successfully.
Solutions:
- The first solution is to make sure that the module being imported and the program that imports the module are in the same directory.
- The second way of avoiding this error is to import the module from its relative path as shown in the illustration given below.
Explanation:
Since the ex module is contained within the UserDefinedModule
directory, so you must import it from this folder itself using the import statement: from UserDefinedModule import ex
Conclusion
In this article, you learned how to avoid the ModuleNotFoundError
in Python.
- While working with an external module, make sure you have installed it properly.
- While working with a user-defined module, you must use your
import
statements properly and ensure that the modules are imported from their relative paths.
Please subscribe and stay tuned for exciting articles. Happy learning! 📚