Stuck on ModuleNotFoundError?
Let’s look at why you’re facing this problem and see how to solve it using some examples. Without further ado, let’s begin!
ModuleNotFoundError is an exception that is raised when Python fails to locate and import a module.
But then this much is obvious from the name of the Exception! The following are a few of the causes why you might be getting the error:
- The module is not installed on your computer
- You have entered the name of the module incorrectly
- Python’s environment is set up incorrectly
Fixing ModuleNotFoundError
If you are looking for a nicely laid-out process for solving this error I recommend checking out our comprehensive article on this topic.
ModuleNotFoundError: A Step By Step Troubleshooting Guide!
If you are looking for the short and sweet version, read on!
Implementing one of the following solutions will resolve or fix the error:
- Verify if the module is present – One of the first steps to do is to check if the module is installed.
pip show pandas
If you get an output like below, then you will have to install the module:
If the module is not installed, you can install it using the ‘pip install’ command as shown below:
pip install pandas
Just replace “pandas” with the name of the module you wish to install.
pip install <module-name>
Once successfully installed, your code should run fine without ModuleNotFoundError!
Pitfalls to avoid
Double-check if you may have made one of the following mistakes and correct it:
- Incorrect spelling: The Python modules are usually acronyms or code names or shortened versions of their full names, so there is a chance that you did not enter the name correctly.
- Incorrect capitalization: This can also happen if you don’t have some “capitalization issue” as everything is case-sensitive in the programming world! For example, spelling it as ‘Pandas’ instead of ‘pandas’.
For example, the following results in a ModuleNotFoundError:
import Time
The ‘time’ module is a standard library module, so it should already be installed along with Python.
This only leaves us to check the spelling, and as you might have guessed, we should’ve spelled it as ‘time’ and not ‘Time’.
The following import statement now runs without any errors:
import time
3. Incorrect Python Environment Usage
If you have multiple environments set up in your system, you may be using the wrong environment in which the module may not exist. Check if you are using the correct environment and then verify if the environment you are using has the module installed!
I hope by now you got the code up and running again!
For the next step in your Python journey I invite you to master some of the most common errors you will run into in your daily life as a Python developer. We have compiled a list of just 7 exceptions that you need to focus on if you wish to gain mastery of Exceptions in Python!
7 Most Common In-Built Exceptions in Python!
If you are a visual learner here is a YouTube video that we made on that same topic!
And with that, I end this article.
I hope you enjoyed reading this article and found it useful!
Feel free to share it with your friends and colleagues!
If your thirst for knowledge has not been quenched yet, here are some related articles that might spark your interest!
Related Articles
7 Most Common In-Built Exceptions in Python!
Python Exceptions: Explained for Beginners!
Python: When To Use Custom Exceptions? Explained!
Python TypeError: A Step By Step Troubleshooting Guide!
Thanks to Namazi Jamal for his contributions in writing this article!